【问题标题】:(Nested)ScrollView and RecyclerView responding to each other(嵌套)ScrollView 和 RecyclerView 相互响应
【发布时间】:2017-03-27 06:41:50
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 你能发布包含布局的完整 xml 文件吗??
  • @SurajMakhija 完成
  • 好的,很好

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


【解决方案1】:

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

关注这个帖子here

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

因此,您可以在此处添加 LinearLayout,它位于您的 ScrollView 中作为您的 recyclerview 的第一个子项(标题),然后我想它应该可以按照您的意愿工作!

这将使您对该屏幕的最终布局只有这样:

  <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 将在 RV 的适配器中膨胀!

【讨论】:

    【解决方案2】:

    您可以使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多