【问题标题】:Scroll behaviour of AndroidX RecyclerView view with NestedScrollView使用 NestedScrollView 滚动 AndroidX RecyclerView 视图的行为
【发布时间】:2019-07-01 13:26:00
【问题描述】:

我正在将我的应用程序从 appcompat 迁移到 AndroidX。除了我无法为androidx.core.widget.NestedScrollViewandroidx.recyclerview.widget.RecyclerView.

我已经尝试添加

setHasFixedSize(true)
setNestedScrollingEnabled(false)

但它不起作用。我的 XML 现在看起来是这样的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    android:clipToPadding="false"
    android:id="@+id/scroll"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingBottom="5dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:background="@color/white"
        android:id="@+id/itenarydays"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <FrameLayout
            android:background="@drawable/roundshape"
            android:layout_gravity="center"
            android:layout_height="56dp"
            android:layout_margin="3dp"
            android:layout_width="56dp"
            android:padding="@dimen/textsise10">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:tint="@color/white"
                app:srcCompat="@drawable/ic_sketch" />
        </FrameLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:animationCache="true"
            android:clipToPadding="false"
            android:id="@+id/list_daycount"
            android:layout_gravity="center"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:nestedScrollingEnabled="false"
            android:orientation="horizontal"
            android:scrollbars="none"

            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"></androidx.recyclerview.widget.RecyclerView>


        <FrameLayout
            android:background="@drawable/shape_redround"
            android:id="@+id/framelyt_add"
            android:layout_gravity="center"
            android:layout_height="56dp"
            android:layout_margin="3dp"
            android:layout_width="56dp"
            android:padding="@dimen/textsise10">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:tint="@color/white"
                app:srcCompat="@drawable/ic_plus" />
        </FrameLayout>
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

【问题讨论】:

  • 不要将 RecyclerView 放在另一个滚动视图中。
  • 在 app-compat 中工作,而不是在 androidx 中
  • @EugenPechanec 如果在 RV 上禁用了嵌套滚动,您绝对可以在其他滚动视图中拥有 recyclerviews。
  • @BrandonMcAnsh 我相当关心 RecyclerView 的回收方面。 ScrollView 不会回收其内容。如果您将垂直 wrap_content RV 放在垂直 SV 中,则不会进行回收。整个视图层次结构将始终在内存中。一个更好的想法是将整个视图层次结构调整为单个垂直 RV 的不同项目类型。

标签: android android-recyclerview androidx nestedscrollview


【解决方案1】:

您似乎需要从 RecyclerView 中删除 layout_behavior 属性,因为您已为它禁用了嵌套滚动(并且已经为 NestedScrollView 设置了它)

【讨论】:

    【解决方案2】:

    使用这个:

    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    

    而不是这个:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    【讨论】:

    • 我很确定这是字符串资源的值。
    • 他们改变了滚动行为并把它放在了一个私有包中,所以你必须通过 "$" 访问它,对我来说,它就像我上面提到的那样工作
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多