PullToRefreshLayout +RecyclerView 实现上拉加载下拉刷新

1 ,xml布局:

com.xinshangyun.app.ui.view.PullToRefreshLayout
    android:id="@+id/fudan_refresh_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/title_bar"
    android:divider="@android:color/transparent"
    android:dividerHeight="@dimen/dp_0"
    android:listSelector="@android:color/transparent">

    

    

    

2,自定义的RecycleView

public class PullableRecyclerView extends RecyclerView  implements Pullable {
    private int lastPosition;
    private boolean mCanPullDown = true;
    private boolean mCanPullUp = true;
    private boolean pd = true;

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

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //判断是否可以下拉刷新
    public void setNo(boolean pd){
        this.pd = pd;
    }

    //是否可以下拉
    public void setCanPullDown(boolean canPullDown) {
        this.mCanPullDown = canPullDown;
    }

    public void setCanPullUp(boolean canPullUp) {
        this.mCanPullUp = canPullUp;
    }

    /**
     * 可以下拉
     *
     * @return
     */
    @Override
    public boolean canPullDown() {
        if(mCanPullDown) {
            if (getChildCount() == 0) {
                return true;
            } else if (getChildAt(0).getTop() >= 0) {
                if (getLayoutManager() instanceof LinearLayoutManager) {
                    int firstVisibleItem = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
                    if (firstVisibleItem == 0) {
                        return true;
                    }
                } else if (getLayoutManager() instanceof GridLayoutManager) {
                    int firstVisibleItem = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
                    if (firstVisibleItem == 0) {
                        return true;
                    }
                }else if(getLayoutManager() instanceof StaggeredGridLayoutManager){
                    //因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组
                    //得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了
//                    int[] lastPositionsDown = new int[((StaggeredGridLayoutManager) getLayoutManager()).getSpanCount()];
                    int lastPositionsDown[] = ((StaggeredGridLayoutManager)getLayoutManager()).findFirstVisibleItemPositions(null);
                    if(findMin(lastPositionsDown)==0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 可以上拉
     *
     * @return
     */
    @Override
    public boolean canPullUp() {
        if(mCanPullUp) {
            if (getChildCount() == 0) {
                return true;
            } else {
                RecyclerView.LayoutManager layoutManager = getLayoutManager();
                if (layoutManager instanceof GridLayoutManager) {
                    //通过LayoutManager找到当前显示的最后的item的position
                    lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                } else if (layoutManager instanceof LinearLayoutManager) {
                    lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    //因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组
                    //得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了
                    int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
                    ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(lastPositions);
                    lastPosition = findMax(lastPositions);
                }
                //时判断界面显示的最后item的position是否等于itemCount总数-1也就是最后一个item的position
                //如果相等则说明已经滑动到最后了 
                if (lastPosition == getLayoutManager().getItemCount()-1 &&  this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
                        >= this.computeVerticalScrollRange()) {
                    return true;
                }
            }
        }
        return false;

    }

    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }
    private int findMin(int[] lastPositions) {
        int min = lastPositions[0];
        for (int value : lastPositions) {
            if (value< min) {
                min = value;
            }
        }
        return min;
    }
}

 

 

你可能感兴趣的:(android)