美文网首页viewAndroid知识Android技术知识
RecyclerRefreshLayout实现下拉刷新和上拉加载

RecyclerRefreshLayout实现下拉刷新和上拉加载

作者: RickGe | 来源:发表于2016-12-19 09:31 被阅读214次

    RecyclerRefreshLayout类的核心代码来自开源中国App。数据获取方式:Bmob云存储。

    01 效果图

    RecyclerRefreshLayout.png

    02 Layout

    activity_main.xml
    <?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 MainActivity

    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, null);
            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){
            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);
                            mRecyclerRefreshLayout.setCanLoadMore(true);
                            mRecyclerRefreshLayout.setOnComplete();
                        }
                        else{
                            mRecyclerRefreshLayout.setCanLoadMore(false);
                            mRecyclerRefreshLayout.setOnComplete();
                        }
    
                    }
                }
            });
        }
    
        @Override
        public void onRefreshing() {
            getDataFromBmob(0);
        }
    
        @Override
        public void onLoadMore() {
            getDataFromBmob(mAdapter.getItemCount());
        }
    }
    

    04 RecyclerRefreshLayout

    RecyclerRefreshLayout的详细代码请看查看源码

    05 数据来源

    数据来自开源中国

    相关文章

      网友评论

        本文标题:RecyclerRefreshLayout实现下拉刷新和上拉加载

        本文链接:https://www.haomeiwen.com/subject/weazmttx.html