下拉加载更多的核心是SwipeRefreshLayout搭配Recyclerview进行使用。布局为
<android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>因为recyclerview自带的是没有刷新功能的。
下拉刷新我们可以调用SwipeRefreshLayout的setOnRefreshListener()来完成。
在回调的方法中完成自己想要的逻辑。一般都是将page设置为1,然后initData()。这个initData()就是进行数据请求的方法。
swipelayout_command.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { page=1; recyclerview.setNoMore(false);//可以加载更多 getRequestData();//请求数据请求 swipelayout_command.setRefreshing(false);//刷新图标消失 } });这里,我推荐一个在github上找到的一个自定义的recyclerview控件,因为其功能比较完善。附上链接:https://github.com/jdsjlzx/LRecyclerView
这个自定义控件包含两种recyclerview,一个LRecyclerview,一个LuRecyclerview。相对而言,LRecyclerview功能更加完善。
我这里用的是LuRecyclerview,搭配swipeRefreshLayout。
<android.support.v4.widget.SwipeRefreshLayout android:layout_below="@id/command_toolbar" android:id="@+id/swipe_command" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.jdsjlzx.recyclerview.LuRecyclerView android:id="@+id/recyclerview_command" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="50dp"> </com.github.jdsjlzx.recyclerview.LuRecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
上拉刷新作者已经写好了API,我们可以直接调用——
recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { /** * 判断现在加载的数据条数是否等于服务器数据总条数 * 如果小于,则page加1,加载新一页的数据 * 如果等于,设置不再加载更多。 */ if (mCurrentCounter < TOTAL_COUNTER) { Log.i("sub", "onLoadMore: click"); Log.i("curr", "onResponse: if "+mCurrentCounter); page++;//page+1,也就是下一页 Log.i("sub", "onCreateView: page"+page); getRequestData(); } else { //the end recyclerview.setNoMore(true);//不再加载更多 } } });
回调方法里面的逻辑:这里我设置了几个常量
/**服务器端一共多少条数据*/ private static final int TOTAL_COUNTER = 10000; /**每一页展示多少条数据*/ private static final int REQUEST_COUNT = 10; /**已经获取到多少条数据了*/ private static int mCurrentCounter = 0;设置这几个常量的作用有两个,一个是确定数据加载完成,显示提示语。另一个是数据加载未完成,加载下一页(这个逻辑放在自动加载更多的回调方法里面)。
这几个常量都可以随自己的需求进行定义。
在自动加载更多的监听事件后设置提示语——
//设置底部加载文字提示 recyclerview.setFooterViewHint("拼命加载中","———— 已经全部为你呈现了 ————","网络不给力啊,点击再试一次吧");接下来看看请求数据的方法的逻辑——
@Override public void onResponse(Call<CommandDetail> call, Response<CommandDetail> response) { if (page==1){ list.removeAll(list); } List<CommandDetail.DataBean> list_count = response.body().getData(); if (REQUEST_COUNT>list_count.size()) { Log.i("curr", "onResponse: count_list size 2"+list_count.size()); recyclerview.setNoMore(true); } list.addAll(response.body().getData()); recyclerview.refreshComplete(REQUEST_COUNT);//完成刷新 //先清空数据,再加载,避免数据重复的问题 adapter.notifyDataSetChanged(); mCurrentCounter+=list_count.size(); }
这里我先进行判断,page是否为1,如果为1,那么先请求数据集合,这么做的目的是为了下拉刷新的时候数据不会重复出现。
然后判断自定义显示的数据条数是否大于加载得到的数据集合的大小,如果是,那么说明数据已经加载完毕,设置setNoMore()为true,这样就会显示提示语。
再将数据添加到集合中;完成刷新;刷新适配器。最后将自定义的当前显示的数据条数的值进行累加。
然后就是绑定适配器。
LinearLayoutManager manager = new LinearLayoutManager(this); recyclerview.setLayoutManager(manager); SongCommandAdapter commandAdapter = new SongCommandAdapter(list, this); adapter = new LuRecyclerViewAdapter(commandAdapter); recyclerview.setAdapter(adapter);这里可以看到,我是将自定义的recyclerviewAdaper作为参数传进了需要的LuRecyclerviewAdapter()中。
这里主要说明的是自定义的适配器完全是按照正常的recyclerviewadaper的逻辑去写。
好了,就这样。关于LuRecyclerview以及LRecyclerview的大家可以点进链接看看。有很多内容,可以添加头布局尾布局,添加分割线等等,自己去了解。这篇文章主要就是说一下关于数据请求的相关逻辑判断。希望你们能看懂。。。。。。。