Android开发中遇到的问题Manba陪你学Androidandroid技术专栏

支持下拉刷新、上拉加载、EmptyView的RecyclerVi

2017-03-15  本文已影响729人  唠嗑008

github地址(Demo下载)
https://github.com/zhouxu88/PullToRefreshRecyclerView

一、简介

RefreshRecyclerView:支持带上拉加载更多、下拉刷新(刷新样式可自定义)、emptyview的RecyclerView

效果:

Paste_Image.png
二、使用步骤

1、import module依赖:pulltorefreshlibrary(在Demo中有,自行下载)

2、在布局文件中将以前使用RecyclerView的地方地换成PullToRefreshRecyclerView控件(说白了,这也是一个自定义RecyclerView)

 <com.androidkun.PullToRefreshRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

3. 初始化PullToRefreshRecyclerView并设置属性和下拉刷新和上拉加载更多的回调

//初始化RecyclerView的配置及上拉加载,下拉刷新
    private void initRecyclerView() {
        recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.recyclerView);
        //添加HeaderView
        View headView = View.inflate(this, R.layout.layout_head_view, null);
        recyclerView.addHeaderView(headView);
        //添加HeaderView
        View headView2 = View.inflate(this, R.layout.layout_head2_view, null);
        recyclerView.addHeaderView(headView2);
        //添加FooterView
        View footerView = View.inflate(this, R.layout.layout_foot_view, null);
        recyclerView.addFooterView(footerView);
        //设置EmptyView
        View emptyView = View.inflate(this, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setEmptyView(emptyView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerViewAdapter(this, data);
        recyclerView.setAdapter(adapter);

        //设置是否开启上拉加载
        recyclerView.setLoadingMoreEnabled(true);
        //设置是否开启下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        //设置是否显示上次刷新的时间
        recyclerView.displayLastRefreshTime(true);
        //设置刷新回调
        recyclerView.setPullToRefreshListener(this);
        //主动触发下拉刷新操作
        //recyclerView.onRefresh();
    }

如果想使用网格列表,则相应设置布局管理者为网格布局管理者就行了

GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2); 
recyclerView.setLayoutManager(gridLayoutManager);

备注:此外也可以通过 setRefreshingResource(int resId)setLoadMoreResource(int resId),这个resId是本地图片的id,来实现自定义刷新箭头和加载的图标。当然了要想实现更复杂的自定义刷新样式,比如其他动画效果,只有进入依赖的pulltorefreshlibrary模块中设置了,这也是为什么笔者用模块依赖的原因,可以通过修改pulltorefreshlibrary代码,简单的实现自定义刷新样式和添加更多的功能

4.处理刷新加载逻辑

 @Override
    public void onRefresh() {
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshComplete();
                //模拟加载数据的情况
                for (int i = 0; i < 4; i++) {
                    data.add(0,new ItemData(R.mipmap.app_refresh_people_0));
                }
                adapter.notifyDataSetChanged();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setLoadMoreComplete(); //加载数据完成
                //模拟加载数据的情况
                for (int i = 0; i < 4; i++) {
                    data.add(new ItemData(R.mipmap.app_refresh_people_0));
                }
                adapter.notifyDataSetChanged();
            }
        }, 2000);
    }
上一篇下一篇

猜你喜欢

热点阅读