XRecycleView的使用心得

项目中很多时候会用到下拉刷新和上拉加载:项目中我们用到的控件是xrecycleView,下面将xrecycleView的使用介绍如下:
首先 XRecycleView  recyclerView ;
1.初始化设置
        recyclerView = view.findViewById( R.id.project_list );
//设置xrecycleView的LinearLayoutManager ;
        LinearLayoutManager layoutManager = new LinearLayoutManager( getActivity() );
        layoutManager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( layoutManager );
        //设置xrecycleView的ProgressStyle
        recyclerView.setLoadingMoreProgressStyle( ProgressStyle.BallRotate );
  //设置xrecycleView的可以加载更多;
        recyclerView.setLoadingMoreEnabled( true );
  //设置xrecycleView的可以下拉刷新;
        recyclerView.setPullRefreshEnabled( true );
        //加载完一页后自动加载下一页数据;
        recyclerView.setLimitNumberToCallLoadMore( 0 );
        recyclerView.getDefaultFootView().setNoMoreHint( "" );
        //
        //        recyclerView.getDefaultFootView().setLoadingHint( "自定义加载中提示" );
        //        recyclerView.getDefaultFootView().setNoMoreHint( "自定义加载完毕提示" );
        recyclerView.setLoadingListener( this );
        projectAdapter = new ProjectAdapter( getActivity(), proListAll );
        recyclerView.setAdapter( projectAdapter );
2. 在数据请求成功后处理数据如下:
1)
    recyclerView.refreshComplete();//设置xrecycleView的刷新完成;
    recyclerView.loadMoreComplete();//设置xrecycleView的加载更多完成;
2)设置空页面,  结合初始化设置 recyclerView.getDefaultFootView().setNoMoreHint( "" );
                   //列表结果总数
                    double sum = tProject.getProjectCount();
                    //无数据页面展示;
                    if (pageNo == 1 && proList.size() == 0 || sum == 0) {
                        mLoadingView.loadSuccess( true );
//                        recyclerView.setVisibility( View.GONE );
                    }
设置无数据页面的显示。这里mLoadingView是公共类中的CommonLoadingView  这个自定义控件的使用需要父布局是相对布局(RelativeLayout)或者FramLayout
  这个CommonLoadingView自定义控件处理了加载失败  空页面的情况  具体使用可以参考项目中ProjectAllFragment.fragement.
3)设置第一页数据不显示提示加载数据更多   注:pageNo是当前加载数据的页面pageNum
                //计算页码总数;
                    int pageSum = (int) Math.ceil( sum / pageSize );
                    if (pageNo >= pageSum) {
                        if (pageNo==1)
                            recyclerView.getDefaultFootView().setNoMoreHint( "" );
                        else
                            recyclerView.getDefaultFootView().setNoMoreHint( "数据加载完毕" );
                        recyclerView.setNoMore( true );
                    }
注意:要先设置hint显示再setNoMore(true) 不然效果出不来  原因吗  不用解释了  呵呵
说明:这个逻辑是基于 下拉刷新和加载更多的逻辑都在请求成功方法中操作 ,当然也可以换个思路  比如我只有在设置加载更多的时候才是用setNoMore方法那么这块的逻辑就不用考虑了。
3.设置xrecycleView的内存释放 (一般会放到destroy方法里面)
// ,use the code below to release XR's memory ;
//  any time,when you finish your activity or fragment,call this below
if(mRecyclerView != null){
    mRecyclerView.destroy(); // this will totally release XR's memory
    mRecyclerView = null;
}

你可能感兴趣的:(android)