微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用支持库23.2.0的Recyclerviews和SwipeRefreshLayout

有没有人想办法让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>

解决方法

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 extends SwipeRefreshLayout和自定义canChildScrollUp.

原文地址:https://www.jb51.cc/android/312798.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐