使用支持库23.2.0的Recyclerviews和SwipeRefreshLayout

时间:2016-02-26 01:16:40

标签: android android-recyclerview android-support-library swiperefreshlayout android-appbarlayout

有没有人想办法让reclerviews,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 个答案:

答案 0 :(得分: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/

创建一个View扩展SwipeRefreshLayout和自定义canChildScrollUp。

答案 1 :(得分:5)

如果您将未实现$http.delete或不是ScrollingView的视图放入AbsListViewSwipeRefreshLayout始终返回false。

因此,在SwipeRefreshLayout#canChildScrollUp()内使用FrameLayout,您有两个问题:

  1. SwipeRefreshLayout不接受后代视图的嵌套滚动操作(请参阅SwipeRefreshLayout)。这会导致SwipeRefreshLayout#onStartNestedScroll(View, View, int) / CoordinatorLayout

  2. 出现问题
  3. AppBarLayout处理触摸事件本身,只要其子项无法向上滚动或者没有正在进行的嵌套滚动(请参阅SwipeRefreshLayoutSwipeRefreshLayout#onInterceptTouchEvent(MotionEvent))。这意味着当“触摸向下移动”时显示微调器。

  4. 您可以使用自己的SwipeRefreshLayout#onTouchEvent(MotionEvent)来覆盖SwipeRefreshLayout来解决此问题。这样,即使直接子视图无法向上滚动,也会接受嵌套滚动:

    SwipeRefreshLayout#onStartNestedScroll(View, View, int)

    顺便说一句,如果你看一下23.1.1中的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; } } 代码,你会看到,canChildScrollUp()检查被删除但是在23.2.0中再次添加。我还删除了SwipeRefreshLayout的支票,因为!mReturningToStart始终为mReturningToStart

答案 2 :(得分:2)

更新为23.2.0后面对相同的issue。这是一个新的错误,已在23.1.1中修复,并再次显示在23.2.0中。

在我的情况下,我降级到23.1.1并且再次正常。因此,我们应该等待新版本的lib或使用workarounds覆盖SwipeRefreshLayout

以下是google bugtracker的链接:RecyclerView v23.2.0 - doesn't play nicely with SwipeRefreshLayout

答案 3 :(得分: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);。这是为了在向下滑动刷新后停止加载动画。