SwipeRefreshLayout+RecyclerView+BaseQuickAdapter,属于你自己的上拉加载下拉刷新控件

开篇:SwipeRefreshLayout,作为google亲生,是一款很好用的控件,美中不足的是,SwipeRefreshLayout只有下拉刷新方法,没有提供上拉加载更多方法,为此配合BaseQuickAdapter使用,就可以满足大部分的上拉加载下拉刷新需求,下面上具体用法:

1,配置module依赖:
 
  
implementation 'com.android.support:support-v4:26.0.2'
implementation 'com.android.support:recyclerview-v7:26.0.2'
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'

2.activity xml布局:

 
  
< android.support.v4.widget.SwipeRefreshLayout
android :id= "@+id/swiperefreshlayout"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
< android.support.v7.widget.RecyclerView
android :id= "@+id/recylerview"
android :layout_width= "match_parent"
android :layout_height= "match_parent" />
android.support.v4.widget.SwipeRefreshLayout >

3.添加自己的Bean实体类

4.自定义Adapter继承BaseQuickAdapter

在实现的方法convert(BaseViewHolder helper, Beans item)中添加数据,示例如下
helper.setText(int id,String str) 添加文本
helper.setImageResource(int id,int resouceId)添加图片
helper.addOnClickListener(int id)添加点击事件
helper.setVisible(int id,boolean isInvisible)设置是否invisible
helper.setGone(int id,boolean isVisiable)true为visible,false为gone

5.activity:

1,初始化数据以及adapter绑定recylerView:

swipeRefreshLayout = view.findViewById(R.id.swiperefreshlayout);
recyclerView = view.findViewById(R.id.recylerview);
itemList = new ArrayList<>();
adapter = new HisOrderAdapter(R.layout.layout_his_order,itemList,status);
recyclerView.setAdapter(adapter);
adapter.bindToRecyclerView(recyclerView);
adapter.setEnableLoadMore(true);
adapter.openLoadAnimation();
adapter.disableLoadMoreIfNotFullPage();
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
adapter.setEmptyView(R.layout.view_nodata);

2,设置adapter加载更多监听事件:

adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
    @Override
    public void onLoadMoreRequested() {
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                    httpLoadMore();
            }
        },1000);


    }
}, recyclerView);

3,设置SwipeRefreshLayout加载数据监听:

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        refresh();
    }
});

4,adapter设置子控件点击监听事件:

adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()){
            case R.id.:
                
                break;
            case R.id.:
                
                break;

        }
    }
});

5.adapter设置自身item点击监听事件:

adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        
    }
});

6.注意事项:

SwipeRefreshLayout加载完毕结束动画方法为:

swipeRefreshLayout.setRefreshing(false);

adapter加载完毕结束动画方法为:

adapter.loadMoreComplete(); adapter.loadMoreEnd();

adapter更新数据方法为:

adapter.setNewData(list);

ok,大功告成。

ps:BaseQuickAdapter github地址为:https://github.com/CymChad/BaseRecyclerViewAdapterHelper。强大的不得了,有兴趣的可以去see see。

   

你可能感兴趣的:(android常用小知识)