【问题标题】:Strange behaviour of recyclerview in nestedscrollview嵌套滚动视图中recyclerview的奇怪行为
【发布时间】:2019-05-01 09:55:38
【问题描述】:

我有如下布局。问题是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"

在 ImageButton 中类似于

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

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

【问题讨论】:

  • 尝试添加recyclerView.setNestedScrollingEnabled(true/false)
  • 那行不通

标签: android xml android-recyclerview nestedscrollview


【解决方案1】:

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

<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>

【讨论】:

  • 使 LinearLayouts 匹配父级不起作用。虽然使用 mipmap 有效。但我想在图像按钮中使用可绘制对象
  • 真正的问题是,如果我使用我的 drawable 与我使用 android 资源或 mipmap,为什么对 recyclerview 很重要
  • 在上面的代码中,内部线性布局作为匹配父级,你可以使用drawable本身。不要只将线性布局作为匹配父级,我也从文本视图中删除了权重。
【解决方案2】:

只需添加即可解决

android:fillViewport="true"

到 NestedScrollView

【讨论】:

    【解决方案3】:

    将 Recycler View 上的 layout_height 设置为 match_parent

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

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2018-02-22
      • 2011-11-04
      相关资源
      最近更新 更多