SwipeRefreshLayout的学习

1、概述

SwipeRefreshLayout是一种下拉刷新的组件,它被放到了V4包中,只允许有一个直接子类。
如下:mTarget是SwipeRefreshLayout的目标View,它是SwipeRefreshLayout的第一个子view,

《SwipeRefreshLayout的学习》 SwipeRefreshLayout源码

2、简单的使用方法

如下:

        SwipeRefreshLayout swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        //setColorSchemeResources()可以改变加载图标的颜色。
        swipeRefreshLayout.setColorSchemeResources(new int[]{R.color.colorAccent, R.color.colorPrimary});
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                 swipeRefreshLayout.setRefreshing(false);
            }
        });

3、显示空布局

但是,当没有数据的时候,怎么显示出数据为空的布局?可以查看google官方的例子,如下:
google官方例子

  • 调用方法:
View mEmptyView = findViewById(android.R.id.empty);
loadMoreListView.setEmptyView(mEmptyView);
swipeRefreshLayout.setSwipeableChildren(R.id.LoadMoreListView,android.R.id.empty);
  • 布局文件
    <com.liyi.loadmorelistview.MultiSwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@+id/LoadMoreListView"
                android:layout_width="match_parent"
                android:divider="@color/colorAccent"
                android:footerDividersEnabled="true"
                android:dividerHeight="2dp"
                android:listSelector="@android:color/transparent"
                android:layout_height="match_parent"/>
            <TextView
                android:id="@android:id/empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="List is empty! Click here to refresh."
                android:layout_gravity="center"/>
        </FrameLayout>
    </com.liyi.loadmorelistview.MultiSwipeRefreshLayout>

如果没有自定义SwipeRefreshLayout,会出现即便没有到顶部,SwipeRefreshLayout也会刷新,正确的情况应该是:当ListView位于顶部时,下拉事件由SwipeRefreshLayout处理,否则由ListView处理

《SwipeRefreshLayout的学习》 只要下拉,就触发刷新

  • 自定义SwipeRefreshLayout

重写canChildScrollUp()方法,返回true,不进行刷新;false进行刷新。

《SwipeRefreshLayout的学习》 onInterceptTouchEvent方法

public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
    private View[] mSwipeableChildren;

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

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

    /**
     * 设置可以触发刷新事件的子view
     * Set the children which can trigger a refresh by swiping down when they are visible. These
     * views need to be a descendant of this view.
     */
    public void setSwipeableChildren(final int... ids) {
        assert ids != null;
        mSwipeableChildren = new View[ids.length];
        for (int i = 0; i < ids.length; i++) {
            mSwipeableChildren[i] = findViewById(ids[i]);
        }
    }

    /**
     * 如果返回false,开始gesture
     * <p/>
     * This method controls when the swipe-to-refresh gesture is triggered. By returning false here
     * we are signifying that the view is in a state where a refresh gesture can start.
     * <p/>
     * <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by
     * default, we need to manually iterate through our swipeable children to see if any are in a
     * state to trigger the gesture. If so we return false to start the gesture.
     */
    @Override
    public boolean canChildScrollUp() {
        if (mSwipeableChildren != null && mSwipeableChildren.length > 0) {
            for (View view : mSwipeableChildren) {
                if (view != null && view.isShown()) {
                    if (view instanceof AbsListView) {
                        return canViewScrollUp(view);
                    }
                }
            }
        }
        return true;
    }

    private static boolean canViewScrollUp(View view) {
        //负数,表示view上滑
        return ViewCompat.canScrollVertically(view, -1);
    }
}


《SwipeRefreshLayout的学习》 没有数据

《SwipeRefreshLayout的学习》 正在加载数据

《SwipeRefreshLayout的学习》 有数据

代码下载

在SwipeRefreshLayout中加入多个子View
Android中SwipeRefreshLayout和listview的冲突解决办法

4、关于SwipeRefreshLayout+ViewPager的滑动冲突

  • 思路:因为是下拉刷新,只有纵向滑动的时候才有效,可以通过判断此时是纵向滑动还是横向滑动(X轴的移动距离大于Y轴的移动距离),纵向滑动就拦截事件,横向滑动不拦截。
  • 可以重写onInterceptTouchEvent()方法,横向滑动不拦截(return false)

代码如下:

public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
    private float startY;
    private float startX;
    // 记录viewPager是否拖拽的标记
    private boolean mIsBeingDragged;
    private final int mTouchSlop;
    private View mSwipeableChildren;

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

    public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float currentX=ev.getX();
        float currentY=ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                startY=currentY;
                startX=currentX;
                mIsBeingDragged=false;
                break;
            case MotionEvent.ACTION_MOVE:
                //如果viewpager正在拖拽中,那么不拦截它的事件,直接return false;
                if(mIsBeingDragged){
                    return false;
                }
                float dx=Math.abs(currentX-startX);
                float dy=Math.abs(currentY-startY);
                // 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理。
                if(dx>dy && dx>mTouchSlop){
                    mIsBeingDragged=true;
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIsBeingDragged=false;
                break;
        }
        // 如果是Y轴位移大于X轴,事件交给swipeRefreshLayout处理。
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 传入SwipeRefreshLayout的Target的id,如R.id.viewPager
     * swipeRefreshLayout.setSwipeableChildren(R.id.viewPager);
     * @param ids
     */
    public void setSwipeableChildren(final int ids) {
        mSwipeableChildren = findViewById(ids);
    }

    @Override
    public boolean canChildScrollUp() {
        return canViewScrollUp(mSwipeableChildren);
    }

    /**
     * 根据传入的ViewPager,查找内部的ListView,并判断其是否到顶部
     * @param view
     * @return
     */
    private static boolean canViewScrollUp(View view) {
        if (view!=null && view instanceof ViewPager) {
            for (int i = 0; i < ((ViewPager) view).getChildCount(); i++) {
                View child = ((ViewPager) view).getChildAt(i);
                if (child.isShown()) {
                    if (child instanceof RelativeLayout) {
                        View subChild = ((RelativeLayout) child).getChildAt(0);
                        if (subChild instanceof AbsListView) {
                            final AbsListView listView = (AbsListView) subChild;
                            return ViewCompat.canScrollVertically(listView, -1);
                        }
                    }
                }
            }
        }
        return false;
    }

}

《SwipeRefreshLayout的学习》 SwipeRefreshLayout+ViewPager—上拉

参考:
Android:SwipeRefreshLayout和ViewPager滑动冲突的原因和正确的解决方式

5、带上拉功能的SwipeRefreshLayout

地址为 :https://github.com/OrangeGangsters/SwipyRefreshLayout

《SwipeRefreshLayout的学习》 带上拉功能的SwipeRefreshLayout

    原文作者:i冰点
    原文地址: https://www.jianshu.com/p/37d1cf72f0a3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞