程序员

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

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

01 效果图

RecyclerRefreshLayout.png

02 Layout

activity_main.xml


    
        
    

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 query = new BmobQuery<>();
        query.include("author");
        query.order("-pubDate");
        query.setSkip(newSkip);
        query.setLimit(10);
        query.findObjects(new FindListener() {
            @Override
            public void done(final List 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 数据来源

数据来自开源中国