20分钟学会下拉刷新、上拉加载,多套布局集成

下拉刷新、上拉加载加多套布局在项目中随处可见,我就提出来我在平常写项目中用到的这套功能。采用的是CommonAdapter+XRecyclerview实现,使用起来特别方便快速,功能完善。

github代码传送门
啥也不说了,先上效果图:

演示效果

分别用到的库

    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.zhy:base-rvadapter:3.0.3'

1.xRecyclerview实现刷新加载。recyclerView.setLoadingListener(this),回调onRefresh和onLoadMore分别去加载对应页数的数据。 recyclerView.refreshComplete()结束刷新,recyclerView.loadMoreComplete()结束加载。

public class MainActivity extends AppCompatActivity implements XRecyclerView.LoadingListener {
    private XRecyclerView recyclerView;
    private ArrayList datas = new ArrayList<>();
    private MultiItemTypeAdapter adapter;
    private int curPage = 0;
    private Handler mHandler = new Handler();

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

        init();
        loadData();
    }

    private void init() {

        recyclerView = (XRecyclerView) findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        adapter = new MyAdapter(getApplicationContext(),datas);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
    }

    private void loadData(){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                final List list = new ArrayList();
                for(int i=curPage*10;i

2.多布局。通常adapter都是使用的CommonAdapter,这里多套布局需要继承MultiItemTypeAdapter实现Adapter,addItemViewDelegate()添加各个item种类,在3个回调方法中分别设置布局、当前positiong是否使用当前该item布局、设置item数据。这里我前5项用item1,后面用item2.

public class MyAdapter extends MultiItemTypeAdapter{

    public MyAdapter(Context context, List datas) {
        super(context, datas);

        addItemViewDelegate(new TypeOne());
        addItemViewDelegate(new TypeTwo());
    }

    public class TypeOne implements ItemViewDelegate {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_one;
        }

        @Override
        public boolean isForViewType(Bean item, int position) {
            if(position < 5){
                return true;
            }else{
                return false;
            }
        }

        @Override
        public void convert(ViewHolder holder, Bean bean, int position) {

        }
    }

    public class TypeTwo implements ItemViewDelegate{

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_two;
        }

        @Override
        public boolean isForViewType(Bean item, int position) { //返回true表示使用该布局,如果有混合用,就会报No ItemViewDelegateManager added that matches position=1 in data source异常
            if(position >= 5){
                return true;
            }else{
                return false;
            }
        }

        @Override
        public void convert(ViewHolder holder, Bean bean, int position) {

        }

    }
}

看,是不是很easy呀。该demo我还会继续完善功能的。

你可能感兴趣的:(20分钟学会下拉刷新、上拉加载,多套布局集成)