嵌套ScrollView中的RecyclerView无法滚动

6

我在NestedScrollView中使用RecyclerView时遇到了滚动问题,除了CollapsingToolbarLayout外。
问题是RecyclerView不能滚动,而CollapsingToolbarLayout也不能展开/折叠。 以下是XML代码:

 <android.support.v4.widget.DrawerLayout 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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:context=".MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="12dp"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleTextAppearance="@color/trans"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/header" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/header_basij" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadeScrollbars="true"
        android:fillViewport="true"
        android:scrollbarDefaultDelayBeforeFade="1000"
        android:scrollbarFadeDuration="2000"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/containerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <pl.droidsonroids.gif.GifImageView
                android:id="@+id/pbar"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:layout_marginTop="8dp"
                android:background="@color/trans"
                android:src="@drawable/loader"
                />

            <LinearLayout
                android:id="@+id/interneterror"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical"
                android:visibility="visible">

                <TextView
                    android:id="@+id/errorhandler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="خطا در اتصال به اینترنت"
                    android:textColor="#212121" />

                <Button
                    android:id="@+id/tryagainme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="سعی دوباره"
                    android:textColor="@color/colorPrimary" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fadeScrollbars="true"
                android:layoutDirection="rtl"
                android:scrollbars="vertical"
                tools:context=".MainActivity"/>

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

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layoutDirection="rtl"
    app:headerLayout="@layout/navigation_header"
    app:itemIconTint="@color/nav_item_icon_tint_color"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/navigation_menu" />

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

感谢您的帮助。

可能是以下问题的重复:如何在NestedScrollView中使用RecyclerView? - smbd uknow
2个回答

3
你需要调用 "recyclerView.setNestedScrollingEnabled(false)"。

@Ram recyclerView.setNestedScrollingEnabled(false) 只能在 API 等级 21 及以上的版本中使用,因此不能解决低版本 API 的问题。 - Edijae Crusar
ViewCompat.setNestedScrollingEnabled(recyclerView, false) - isabsent

0
原因是因为 NestedScrollView 在接收到来自 RecyclerView 的滚动事件时不会调用 AppBarLayout.Behavour 类。也就是说,当 RecyclerView 中发生滚动时,RecyclerView 会将滚动进度传递给 NestedScrollView。虽然 NestedScrollView 会接收到滚动事件,但却不会对其进行处理。

NestedScrollView 类内部

@Override
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){
         //Do nothing
}

为了解决这个问题并使得AppBarLayout在滚动RecyclerView时能够展开/折叠,只需要创建一个自定义类,继承NestedScrollView并重写上述方法,并调用dispatchNestedPreScroll()方法来通知AppBarLayout滚动事件并响应它。
  public class CustomNestedScrollView extends NestedScrollView{

    public CustomNestedScrollView(Context context) {
    super(context);
    }

    public CustomNestedScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    }

  @Override
  public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){
    dispatchNestedPreScroll(dx,dy,consumed,null);
  }
}

然后在你的layout.xml中使用这个类

<com.my.package.CustomNestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

   /*
       CONTENT
   */

</LinearLayout>

这对我没用! 这很复杂(至少对我来说是) 而且这似乎有点过度编码。我解决了这个问题,而不需要做所有这些... 我鼓励你们读者寻找更简单的方法。因为确实有! - eRaisedToX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接