【问题标题】:Is it possible to make layout scrollable without using the scroll views(ScrollView, NestedScrollVIew...)?是否可以在不使用滚动视图(ScrollView、NestedScrollVIew...)的情况下使布局可滚动?
【发布时间】:2020-11-18 14:58:17
【问题描述】:

你好, 下面是我的布局:

  <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/layout_1"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/color_dark_red">

            </RelativeLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="640dp"
                android:background="@color/color_black"
                android:layout_below="@+id/layout_1" />
            
            <RelativeLayout
                android:id="@+id/layout_2"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/recycler_view"
                android:background="#0f0">

            </RelativeLayout>
            
        </RelativeLayout>

截图下方:

我想在用户向下滚动 recyclerView 时隐藏 layout_1,或者在用户向上滚动 recyclerView 时隐藏 layout_2

根据文档: 切勿将 RecyclerView 或 ListView 添加到滚动视图。这样做会导致用户界面性能不佳和用户体验不佳。

我也不想使用NestedScrollView,因为它会在background Thread 中更新recyclerView 时禁用(或删除)DiffUtil 工作,如以下问题所示:

Using DiffUtil, How to prevent blocking the main thread when updating the recyclerView contained in a NestedScrollView?

如何使recycler_view 的向下滚动隐藏layout_1 并向上滚动隐藏layout_2 不使用 ScrollView 或 NestedScrollView ?

谢谢。

【问题讨论】:

  • 在您的情况下,我认为可以在您的 layout_1 和 layout_2 中模拟折叠视图效果。当用户滚动列表时,您可以使用 RecyclerView 侦听器来处理,然后在视图中应用动画来隐藏或显示它们。

标签: android android-layout android-recyclerview android-scrollview android-nestedscrollview


【解决方案1】:

在这种情况下你可以使用 recyclerView 滚动监听器

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

    if (!recyclerView.canScrollVertically(1)) {
        // show the last item 
    }
 }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多