nestedscrollview中recyclerview的奇怪行为

时间:2019-05-01 09:55:39

标签: android xml android-recyclerview nestedscrollview

我的布局如下。问题在于,recyclerview高度仅显示一项。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="@dimen/activity_horizontal_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Live Transactions"
                android:textStyle="bold"/>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_refresh"
                android:background="@android:color/transparent"/>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

奇怪的是,当我更改属性

app:srcCompat="@drawable/ic_refresh"
在图像按钮中

类似于

app:srcCompat="@android:drawable/ic_menu_search"

recyclerview高度变为大多数物品所显示的正常高度。 ImageButton位于RecyclerView上方的布局中。为什么会这样?

3 个答案:

答案 0 :(得分:0)

您必须将内部线性布局作为匹配父级

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Live Transactions"
                android:textStyle="bold"/>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_launcher"
                android:background="@android:color/transparent"/>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

答案 1 :(得分:0)

只需添加即可解决

android:fillViewport="true"

到NestedScrollView

答案 2 :(得分:-1)

使“回收者视图”中的layout_height成为match_parent

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
相关问题