使用支持库23.2.0的Recyclerviews和SwipeRefreshLayout

6

有没有人已经找到了一种方法,让recyclerviews、AppbarLayouts和SwipeRefreshLayout在23.2上一起工作?我认为我正在使用一个相当标准的方法,但是当尝试向上移动recyclerview时,swiperefreshlayout会不断地抓取滚动手势。

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="?attr/toolbar_theme"
            app:layout_scrollFlags="scroll|enterAlways"
            android:elevation="4dp" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/fragment_container"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--fragment goes here -->
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

其中包含以下内容

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/window_background">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_marginTop="-4dp"
        android:layout_marginBottom="-8dp"
        android:elevation="17dp"
        android:indeterminate="true"
        android:visibility="invisible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
4个回答

6

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ImprovedSwipeLayoutAttrs">
        <attr name="scrollableChildId" format="reference" />
    </declare-styleable>
</resources>

layout.xml

<in.nerd_is.inactive_weibo.ui.ImprovedSwipeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:isl="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/md_blue_grey_50"
    isl:scrollableChildId="@+id/list_statuses"
    tools:context="in.nerd_is.inactive_weibo.ui.StatusesFragment" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/list_statuses"
            android:minHeight="?android:attr/listPreferredItemHeight"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:clipToPadding="false"
            android:divider="@android:color/transparent"
            android:dividerHeight="12dp"/>

        <com.melnykov.fab.FloatingActionButton
            android:id="@+id/button_floating_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@drawable/ic_md_create"
            fab:fab_colorNormal="@color/md_blue_400"
            fab:fab_colorPressed="@color/md_blue_grey_500"/>
    </FrameLayout>

</in.nerd_is.inactive_weibo.ui.ImprovedSwipeLayout>

ImprovedSwipeLayout.java

public class ImprovedSwipeLayout extends SwipeRefreshLayout {

    private static final String TAG = ImprovedSwipeLayout.class.getCanonicalName();
    private int mScrollableChildId;
    private View mScrollableChild;

    public ImprovedSwipeLayout(Context context) {
        this(context, null);
    }

    public ImprovedSwipeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ImprovedSwipeLayoutAttrs);
        mScrollableChildId = a.getResourceId(R.styleable.ImprovedSwipeLayoutAttrs_scrollableChildId, 0);
        mScrollableChild = findViewById(mScrollableChildId);
        a.recycle();
    }

    @Override
    public boolean canChildScrollUp() {
        ensureScrollableChild();

        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mScrollableChild instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mScrollableChild;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return mScrollableChild.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mScrollableChild, -1);
        }
    }

    private void ensureScrollableChild() {
        if (mScrollableChild == null) {
            mScrollableChild = findViewById(mScrollableChildId);
        }
    }

}

这篇文章来源于http://nerd-is.in/2014-09/add-multi-child-view-in-swiperefreshlayout/

创建一个继承自SwipeRefreshLayout的View,并自定义canChildScrollUp方法。


5
如果您将一个未实现ScrollingView或不是AbsListView的视图放入SwipeRefreshLayout中,SwipeRefreshLayout#canChildScrollUp()总是返回false。
因此,通过在SwipeRefreshLayout内部使用FrameLayout,您会遇到两个问题:
  1. SwipeRefreshLayout不接受后代视图的嵌套滚动操作(请参见SwipeRefreshLayout#onStartNestedScroll(View, View, int))。这会导致与CoordinatorLayout/AppBarLayout相关的问题。

  2. 只要其子项无法向上滚动或不存在嵌套滚动正在进行(请参见SwipeRefreshLayout#onInterceptTouchEvent(MotionEvent)SwipeRefreshLayout#onTouchEvent(MotionEvent)),SwipeRefreshLayout就会自行处理触摸事件。这意味着当"触摸向下移动"时,旋转器会显示。

您可以通过使用自己的SwipeRefreshLayout来解决这个问题,它覆盖了SwipeRefreshLayout#onStartNestedScroll(View, View, int)。这样,即使直接子视图无法向上滚动,也会接受嵌套滚动:
public class SwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout {

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

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

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return isEnabled()
                && !isRefreshing()
                && (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }
}

顺便提一句,如果你看23.1.1版本的SwipeRefreshLayout代码,你会发现它删除了canChildScrollUp()检查,但在23.2.0版本中又加回来了。我也删除了对!mReturningToStart的检查,因为mReturningToStart始终为false


你不需要再使用这个变通方法了,因为在23.3.0版本中已经修复了这个问题。 - segoh

2
更新到 23.2.0 后遇到了相同的 问题。这是一个已经在 23.1.1 中修复过的旧 - 新 bug,在 23.2.0 中再次出现。

在我的情况下,我降级到 23.1.1,一切都恢复正常了。因此,我们应该等待新版本的库或使用 解决方法,覆盖 SwipeRefreshLayout


这是谷歌错误跟踪器的链接:RecyclerView v23.2.0 - 与SwipeRefreshLayout不兼容

0
我会把我的解决方案添加到这里。我已经尝试过了,在第23级可以运行。大部分代码将在您的xml文件中。像这样。
xml:
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways" />

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipe_to_refresh"
            android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

不要忘记添加:swipeRefreshLayout.setRefreshing(false);。这是为了在下拉刷新后停止加载动画。

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