Widget - RecyclerRefreshLayout

2016-12-20  本文已影响117人  RickGe

本文未投入专题

该控件继承Android标准的下拉刷新控件SwipeRefreshLayout,实现了RecyclerView的下拉刷新和上拉加载功能。

01 RecyclerRefreshLayout.java

public class RecyclerRefreshLayout extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecycleView;
    private SuperRefreshLayoutListener listener;

    private int mTouchSlop;
    private boolean mIsOnLoading = false;
    private boolean mCanLoadMore = true;
    private int mDownY;
    private int mMoveY;

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

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

    @Override
    public void onRefresh() {
        if (listener != null && !mIsOnLoading) {
            listener.onRefreshing();
        } else
            setRefreshing(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 初始化ListView对象
        if (mRecycleView == null) {
            getRecycleView();
        }
    }

    /**
     * 获取RecyclerView
     */
    private void getRecycleView() {
        if (getChildCount() > 0) {
            View childView = getChildAt(0);
            if (!(childView instanceof RecyclerView)) {
                childView = findViewById(R.id.recyclerView);
            }
            if (childView != null && childView instanceof RecyclerView) {
                mRecycleView = (RecyclerView) childView;
                mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        if (canLoad() && mCanLoadMore) {
                            loadData();
                        }
                    }
                });
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                mMoveY = (int) event.getRawY();
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 是否可以加载更多, 条件是到了最底部
     *
     * @return isCanLoad
     */
    private boolean canLoad() {
        return isScrollToBottom() && !mIsOnLoading && isPullUp();
    }

    /**
     * 如果到了最底部,而且是上拉操作.那么执行onLoad方法
     */
    private void loadData() {
        if (listener != null) {
            setOnLoading(true);
            listener.onLoadMore();
        }
    }

    /**
     * 是否是上拉操作
     *
     * @return isPullUp
     */
    private boolean isPullUp() {
        return (mDownY - mMoveY) >= mTouchSlop;
    }

    /**
     * 设置正在加载
     *
     * @param loading 设置正在加载
     */
    public void setOnLoading(boolean loading) {
        mIsOnLoading = loading;
        if (!mIsOnLoading) {
            mDownY = 0;
            mMoveY = 0;
        }
    }

    /**
     * 判断是否到了最底部
     */
    private boolean isScrollToBottom() {
        return (mRecycleView != null && mRecycleView.getAdapter() != null)
                && getLastVisiblePosition() == (mRecycleView.getAdapter().getItemCount() - 1);
    }

    /**
     * 刷新或加载结束记得调用
     */
    public void setOnComplete() {
        setOnLoading(false);
        setRefreshing(false);
    }

    /**
     * 是否可加载更多
     *
     * @param mCanLoadMore 是否可加载更多
     */
    public void setCanLoadMore(boolean mCanLoadMore) {
        this.mCanLoadMore = mCanLoadMore;
    }

    /**
     * 获取RecyclerView可见的最后一项
     *
     * @return 可见的最后一项position
     */
    public int getLastVisiblePosition() {
        int position;
        if (mRecycleView.getLayoutManager() instanceof LinearLayoutManager) {
            position = ((LinearLayoutManager) mRecycleView.getLayoutManager()).findLastVisibleItemPosition();
        } else if (mRecycleView.getLayoutManager() instanceof GridLayoutManager) {
            position = ((GridLayoutManager) mRecycleView.getLayoutManager()).findLastVisibleItemPosition();
        } else if (mRecycleView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) mRecycleView.getLayoutManager();
            int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
            position = getMaxPosition(lastPositions);
        } else {
            position = mRecycleView.getLayoutManager().getItemCount() - 1;
        }
        return position;
    }

    /**
     * 获得最大的位置
     *
     * @param positions 获得最大的位置
     * @return 获得最大的位置
     */
    private int getMaxPosition(int[] positions) {
        int maxPosition = Integer.MIN_VALUE;
        for (int position : positions) {
            maxPosition = Math.max(maxPosition, position);
        }
        return maxPosition;
    }

    /**
     * 添加加载和刷新
     *
     * @param listener add the listener for SuperRefreshLayout
     */
    public void setSuperRefreshLayoutListener(SuperRefreshLayoutListener listener) {
        this.listener = listener;
    }

    public interface SuperRefreshLayoutListener {
        // 下拉刷新
        void onRefreshing();
        // 上拉加载
        void onLoadMore();
    }
}

02 layout中使用RecyclerRefreshLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.rickge.learnbmob.widget.RecyclerRefreshLayout
        android:id="@+id/recyclerRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"/>
    </com.rickge.learnbmob.widget.RecyclerRefreshLayout>
</RelativeLayout>

03 代码控制RecyclerRefreshLayout

public class MainActivity extends AppCompatActivity implements RecyclerRefreshLayout.SuperRefreshLayoutListener{
    private Context mContext;
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    RecyclerView mRecyclerView;
    TweetAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;

        initWidget();
        initData();
    }

    private void initData() {
        mAdapter = new TweetAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerRefreshLayout.setRefreshing(true);
        getDataFromBmob(0);
    }

    private void initWidget() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerRefreshLayout = (RecyclerRefreshLayout) findViewById(R.id.recyclerRefreshLayout);
        mRecyclerRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.swipe_background_color);
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,
                R.color.swipe_color_2,
                R.color.swipe_color_3,
                R.color.swipe_color_4);
        mRecyclerRefreshLayout.setProgressViewEndTarget(true, 200);
        //mRecyclerRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        //mRecyclerRefreshLayout.setPadding(20, 20, 20, 20);
        //mRecyclerRefreshLayout.setProgressViewOffset(true, 100, 200);
        //mRecyclerRefreshLayout.setDistanceToTriggerSync(50);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
    }

    private RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    // 获取Bmob云存储数据
    private void getDataFromBmob(final int newSkip){
        if(newSkip != 0){
            mAdapter.setState(TweetAdapter.STATE_LOADING);
        }

        BmobQuery<Tweet> query = new BmobQuery<>();
        query.include("author");
        query.order("-pubDate");
        query.setSkip(newSkip);
        query.setLimit(10);
        query.findObjects(new FindListener<Tweet>() {
            @Override
            public void done(final List<Tweet> list, BmobException e) {
                // 网络请求成功
                if(e == null && list != null){
                    // 有新数据
                    if(list.size() > 0){
                        if(newSkip == 0){
                            mAdapter.clear();
                        }

                        mAdapter.addAll(list);
                        mAdapter.setState(TweetAdapter.STATE_HIDE);
                        mRecyclerRefreshLayout.setCanLoadMore(true);
                        mRecyclerRefreshLayout.setOnComplete();
                    }
                    // 没有数据
                    else{
                        mAdapter.setState(TweetAdapter.STATE_NO_MORE);
                        mRecyclerRefreshLayout.setCanLoadMore(false);
                        mRecyclerRefreshLayout.setOnComplete();
                    }

                }
                // 网络请求失败
                else {
                    mAdapter.setState(TweetAdapter.STATE_LOAD_ERROR);
                    mRecyclerRefreshLayout.setOnComplete();
                }
            }
        });


    }

    // 下拉刷新
    @Override
    public void onRefreshing() {
        getDataFromBmob(0);
    }

    // 上拉加载
    @Override
    public void onLoadMore() {
        getDataFromBmob(mAdapter.getItemCount());
    }
}

04 参考来源

程序代码参考自开源中国App。数据获取方式:Bmob云存储。

上一篇下一篇

猜你喜欢

热点阅读