美文网首页
android列表数据展示处理逻辑

android列表数据展示处理逻辑

作者: 背锅TV丶伴奏大师 | 来源:发表于2022-05-23 17:47 被阅读0次

    1.使用的是BaseQuickAdapter+RecyclerView+SmartRefreshLayout

    //BaseQuickAdapter
    api 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.49'
    //SmartRefreshLayout
    implementation  'com.scwang.smart:refresh-layout-kernel:2.0.3'      //核心必须依赖
        implementation  'com.scwang.smart:refresh-header-classics:2.0.3'    //经典刷新头
        implementation  'com.scwang.smart:refresh-header-material:2.0.0-alpha-1'    //谷歌刷新头
    

    2.布局

    <?xml version="1.0" encoding="utf-8"?>
    <com.scwang.smart.refresh.layout.SmartRefreshLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/smartRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/recyclerView"
            android:overScrollMode="never"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </com.scwang.smart.refresh.layout.SmartRefreshLayout>
    

    3.初始化和处理逻辑

    public abstract class BaseListActivity<T,K extends BaseQuickAdapter> extends BaseActivity{
    protected K adapter;
    protected List<T> list=new ArrayList<>();
    protected int pageNum = 1;//页码
    protected int pageSize=10;//每页的个数
    protected RecyclerView.LayoutManager layoutManager;
    protected View emptyView;//空数据布局
    protected abstract void loadMore();
    protected abstract void refresh();
    protected RecyclerView recyclerView;
    protected SmartRefreshLayout smartRefreshLayout;
    protected View headView;//头部布局
    protected void initRecyclerViewsWithEmptyView(){
            if(adapter==null){
                //请先初始化adapter
                //throw new NullPointerException("请先初始化adapter");
                return;
            }
            recyclerView=findViewById(R.id.recyclerView);
            smartRefreshLayout=findViewById(R.id.smartRefreshLayout);
            //smartRefreshLayout.setHasFixedSize(true);
            smartRefreshLayout.setNestedScrollingEnabled(false);
            //默认LinearLayoutManager
            if(layoutManager==null){
                layoutManager=new LinearLayoutManager(mContext);
            }
            recyclerView.setLayoutManager(layoutManager);
            if(headView!=null){
                adapter.addHeaderView(headView);
            }
    
           if(emptyView==null){
               // ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                View defaultEmptyView = LayoutInflater.from(this).inflate(R.layout.common_layout_cart_empty_error, null);
                //defaultEmptyView.setLayoutParams(layoutParams);
                TextView emptyText = defaultEmptyView.findViewById(R.id.tv_empty_msg);
                emptyText.setText(emptyStr);
                adapter.bindToRecyclerView(recyclerView);
                adapter.setEmptyView(defaultEmptyView);
                //adapter.setEmptyView(R.layout.common_layout_cart_empty_error,recyclerView);
            }else{
                adapter.bindToRecyclerView(recyclerView);
                adapter.setEmptyView(emptyView);
            }
            recyclerView.setAdapter(adapter);
            smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
            smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
            smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                    refresh();
                }
            });
            smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshlayout) {
                    //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                    //当第一页充满时才加载第二页
                    if(list.size()>=pageSize){
                        loadMore();
                    }else{
                        smartRefreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            });
            //smartRefreshLayout.setOnRefreshListener(refreshlayout -> refresh());
            //smartRefreshLayout.setOnLoadMoreListener(refreshlayout -> loadMore());
    //与上面一个二选一,
            adapter.setOnLoadMoreListener(() -> {
                loadMore();
    //        adapter.loadMoreFail();
    //        adapter.loadMoreEnd();
    //        adapter.loadMoreComplete();
            });
        }
        /**
         * 回调失败的处理,针对有上拉加载,下拉刷新的情况
         */
        protected void showErrorView(boolean isRefresh){
            if(isRefresh){//pageNum == 1
                smartRefreshLayout.finishRefresh();
            }else{
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        /**
         * 成功回调的处理
         * @param isRefresh
         * @param result
         */
        protected void showSuccessView(boolean isRefresh,List<T> result){
            //第一种写法
            if(isRefresh){
                //smartRefreshLayout.resetNoMoreData();
                smartRefreshLayout.setNoMoreData(false);
                smartRefreshLayout.finishRefresh();
                list.clear();
                if (null != result && result.size() != 0) {
                    //smartRefreshLayout.setVisibility(View.VISIBLE);
                    if (result.size() < pageSize){
                        smartRefreshLayout.setEnableLoadMore(true);
                    }else {
                        smartRefreshLayout.setEnableLoadMore(false);
                    }
                    list.addAll(result);
                }
                adapter.notifyDataSetChanged();
            }else{//加载更多
                //smartRefreshLayout.setVisibility(View.VISIBLE);
                if (null != result && result.size() != 0) {
                    if (result.size() < pageSize){
                        smartRefreshLayout.finishLoadMoreWithNoMoreData();
                    }else {
                        smartRefreshLayout.finishLoadMore();
                    }
                    list.addAll(result);
                    adapter.notifyDataSetChanged();
                }else{
                    //smartRefreshLayout.finishLoadMoreWithNoMoreData();
                    smartRefreshLayout.finishLoadMore(100,true,true);
                }
            }
            //第二种写法
            if (pageNum == 1){
                list.clear();
                smartRefreshLayout.finishRefresh();
                smartRefreshLayout.setEnableLoadMore(true);
            }else{
                smartRefreshLayout.finishLoadMore();
            }
            if(result!=null){
                if (result.size() <pageSize) {
                    smartRefreshLayout.setEnableLoadMore(false);
                    smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
                list.addAll(result);
            }else {
                if(pageNum>1){
                    smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        }
    }
    

    4.调用示例 kotlin:

    //初始化
    adapter = FactoryListAdapter(list)
    initRecyclerViewsWithEmptyView()
    //模拟网络请求
    private fun getList(isRefresh: Boolean) {
    onSuccess(data){
    showSuccessView(isRefresh, data)
    }
    onError(){
    showErrorView(isRefresh)
    }
    }
    override fun loadMore() {
            pageNum++
            getList(false)
        }
    
        override fun refresh() {
            pageNum = 1
            getList(true)
        }
    

    相关文章

      网友评论

          本文标题:android列表数据展示处理逻辑

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