首页 > 解决方案 > 滚动recylerview时如何使recylerview上方的文本滚动

问题描述

我有一个布局,它下面有标题 textview 和 recylerview。这一切都很好。但我试图让整个事情都可以滚动。这意味着如果我在添加新项目时向下滚动 rcylerview,我希望标题 textview 向上滚动(消失),就好像它是 recylerview 的一部分

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"  xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:text="Near me"
                    android:textColor="@color/White"
                    android:textSize="21sp"
                    android:textStyle="bold" />

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rvVideos"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

我知道我可以伪造它,因此 recylerview 的第 1 项具有不同的布局,即标题文本,但我希望有更好的选择,因此我不必处理项目数量和操纵位置的边缘情况

标签: androidandroid-layout

解决方案


您需要将 textview 和 recycler 视图都包装在NestedScrollView

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <TextView
                    android:id="@+id/textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"                   
                    android:text="Textview"
                    android:layout_marginTop="10dp"/>

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

            </LinearLayout>

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

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

</android.support.design.widget.CoordinatorLayout>

我希望这会有所帮助......干杯


推荐阅读