(嵌套)ScrollView和RecyclerView互相响应

时间:2017-03-27 06:41:51

标签: java android android-recyclerview android-xml android-nestedscrollview

我在屏幕上滚动时遇到了一些问题。布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

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

    <LinearLayout
      android:id="@+id/my_account_parent_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <!--- Views omitted --->
    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

问题是recyclelerview不能在Scrollview / NestedScrollView中,这就是为什么它在scrollview之外。现在我需要包含在LinearLayout中的视图,当用户滚动任何视图(RecyclerView和它上面的所有其他视图)时,它包围在NestedScrollView中以向上滚动。即使其他视图周围有滚动视图,他们也没有意识到他们可以滚动屏幕,即使recyclerview在屏幕外有内容?

由于

2 个答案:

答案 0 :(得分:0)

是的,在RecyclerView内添加ScrollView不是一个好习惯,但您可以向recyclerView添加自定义标头。

关注此帖here

这是一个很好的SO帖子,用于向RecyclerView添加标题。

所以在这里你可以添加LinearLayout作为你回收者视图的第一个孩子(标题)的ScrollView,然后我想它应该按照你的意愿工作!

这将使您在该屏幕的最终布局:

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

如果按照帖子在RV的适配器中对LinearLayout进行充气!

答案 1 :(得分:0)

您可以使用CoordinatorLayoutapp:layout_scrollFlags。当recyclerview向上滚动时,AppBarLayout内的布局将向上滚动。

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <LinearLayout
      android:id="@+id/my_account_parent_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_scrollFlags="scroll|enterAlways">


    </LinearLayout>

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

    <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</android.support.design.widget.CoordinatorLayout>
相关问题