NestedScrollViewLayout 内的 SwipeRefreshLayout 内的 RecyclerView 无法滚动

Posted

技术标签:

【中文标题】NestedScrollViewLayout 内的 SwipeRefreshLayout 内的 RecyclerView 无法滚动【英文标题】:RecyclerView inside SwipeRefreshLayout inside NestedScrollViewLayout cannot scroll 【发布时间】:2016-10-24 06:10:06 【问题描述】:

我有一个带有折叠工具栏的活动和一个带有FrameLayout 的嵌套滚动视图,我将片段放入其中。最初,我在其中放置了一个带有 CardView FragmentA

效果很好。当我单击一个按钮时,我将 FragmentB 替换为另一个包含 RecyclerView 的按钮。

当我添加 FragmentB 时,我无法滚动到列表底部。

宿主活动布局:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_
    android:layout_
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">


    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_
        android:layout_
        app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

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

<android.support.v4.widget.NestedScrollView
    android:layout_
    android:layout_
    android:layout_gravity="fill_vertical"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >

    <!-- the layout which will be the content of the activity (which will be hosted inside the drawer (NOT the list of the drawer)) -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    </FrameLayout>

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

FragmentB 布局:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/contentView"
    android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_users"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_

        android:layout_ />

    <include
        layout="@layout/view_progress"
        />

    <include
        layout="@layout/view_retry"
        />

    <!--</LinearLayout>-->

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

【问题讨论】:

已经在第一个答案中回答... 将回收站视图的高度更改为 match_parent 【参考方案1】:

您将RecyclerView 放在NestedScrollView 中。 我认为更好的解决方案是拥有NestedScrollViewRecyclerView,但不能同时拥有两者,因为RecyclerView 已经在实施NestedScrollingChild

我的应用中有类似的布局。我也这样做了——将 RecyclerView 放到 FrameLayout 中,然后放到 NestedScrollView 中。它停止正常工作。没有 NestedScrollView 一切正常。

【讨论】:

问题不是RecyclerView with NestedScrollview,而是SwipeRefreshLayout with NestedScrollviewRecyclerViewNestedScrollview 可以一起工作。【参考方案2】:

两件事:

首先,如果您想在 Activity 中实现滚动行为,则不必将片段容器嵌入到 NestedScrollView

FrameLayout 直接放入带有layout_behavior 属性的Coordinator 布局中,然后简单地放入带有滚动视图的任何片段(并且它不必具有layout_behavior 属性)。

所以在你的例子中你应该有片段:

FragmentA 在 NestedScrollView 内具有 CardView 布局 FragmentB 在 SwipeRefreshLayout 中使用 RecyclerView

另一个问题是您在SwipeRefreshLayout 中放置了多个孩子,并且如文档中所述,它应该只有 一个 直接孩子。 source

【讨论】:

【参考方案3】:

我认为这是滚动事件问题,customview 扩展了 ListView 或 RecycleView。 这是我的解决方案:

public class RewriteListView extends ListView 


    public RewriteListView(Context context) 
            super(context);
    


    public RewriteListView(Context context, AttributeSet attrs) 
            super(context, attrs);
    


    public RewriteListView(Context context, AttributeSet attrs, int defStyle) 
            super(context, attrs, defStyle);
    


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                            MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
    

要开心:)

【讨论】:

【参考方案4】:

已经回答了!两个可滚动元素(例如您问题中的 RecycleView 和 NestedScrollView )不能一起工作。

编辑您的活动布局。

<ParentLayout>

.......

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_
    android:layout_
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">


    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_
        android:layout_
        app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

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

//THIS FRAGMENT WILL BE REPLACED!
<FrameLayout
        android:id="@+id/frame_container"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    </FrameLayout>

.......
</ParentLayout>

并且默认情况下,在此活动的第一次运行中 - 替换上面的 FrameLayout - 使用 NestedScrollView 到您的第一个片段(您需要创建新的片段进行替换),然后在您的问题中单击替换为片段。

【讨论】:

问题不是RecyclerView with NestedScrollview,而是SwipeRefreshLayout with NestedScrollviewRecyclerViewNestedScrollview 可以一起工作。

以上是关于NestedScrollViewLayout 内的 SwipeRefreshLayout 内的 RecyclerView 无法滚动的主要内容,如果未能解决你的问题,请参考以下文章