RecyclerView is almost used in every APP. Whenever a list is used, it will use paging loading for data request and loading. There is a pagination library available on Android, but kotlin is the only one who feels good about it. Java version also has a lot of very powerful third-party libraries, BaseRecyclerViewAdapterHelper this library is one of my most comfortable to use paging library, it also contains a variety of powerful functions: drag grouping, sorting, animation, because of the powerful function, also is relatively large amount of code. But most of the time what we want is the page to load, so the reference BaseRecyclerViewAdapterHelper wrote the paging load library, only paging. (It can be copied, it can be simplified, but it also adds personal understanding.) The library is relatively BaseRecyclerViewAdapterHelper only has two advantages:

  • Small amount of code
  • BaseRecyclerViewAdapterHelper when data is less than a screen still shows that load more initialization time and page displays loadmoewView (although provides apis to hide, but took a long comments and document all didn’t know how to use), The library does not display loadmoreView for the first load and for less than one screen of data

Gradle reference

Implementation ‘com. Maxcion: pageloadadapter: 1.0.0’

Project address: github.com/Likeyong/Pa…

Single column paging load

/ / must be in PageLoadRecyclerVewAdapter < String > generic parameters specify the data source inside the item format public class SimpleAdapter extends PageLoadRecyclerVewAdapter<String> { public SimpleAdapter(List<String> dataList) { super(dataList); } @override protected void convert(BaseViewHolder holder, String item) {holder.settext (r.idext, item); } Override protected int getItemLayoutId() {return r.layout.item_simple; }}Copy the code

Now that the Adapter has been implemented, you need to turn on the Adapter pagination load function

public class SingleColumnActivity extends BaseActivity<String> implements IOnLoadMoreListener { @Override protected void  onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single_column); RecyclerView rv = findViewById(R.id.rv); // Instantiate adapter mAdapter = new SimpleAdapter(null); Madapter.setloadmoreview (new CommonLoadMoreView())); / / set the slide to the bottom for more load callback mAdapter. SetOnLoadMoreListener (this); rv.setAdapter(mAdapter); rv.setLayoutManager(new LinearLayoutManager(this)); request(); } @Override public void onLoadMoreRequested() { request(); Override protected List<String> convertRequestData(List<String> convertRequestData) {return originData; }}Copy the code

The second step, RecyclerView also opened the pagination loading function, the third part is according to the data returned by the interface to determine whether the loading failed, the addition succeeded or the end of the loading (no more data need to load)

protected void request() { NetWorkRequest.request(mAdapter.getDataSize() / PAGE_SIZE + 1, mFailCount, new NetWorkRequest.Callback() { @Override public void onSuccess(List<String> result) { List<T> finalResult = convertRequestData(result); If (result.size() >= PAGE_SIZE){if(adapter.getdatasize () == 0){if(adapter.getdatasize () == 0){ So here we use setNewData() madapter.setnewData (finalResult); Madapter.adddatalist (finalResult); madapter.adddatalist (finalResult); } // Call adapter here. Inform list refresh footview loadMoreComplete (true) function, this parameter must be the true mAdapter. LoadMoreComplete (true); }else {// Madapter.loadMoreEnd (true);}else {// Madapter.loadmoreEnd (true); } } @Override public void onFail() { mFailCount++; // Request failure notification recyclerView refresh footView state madapter.loadMorefail (true); }}); }Copy the code

Above is the mock interface request I wrote. Don’t worry about the rest of the code, just focus on the logic inside the onSuccess and onFail callbacks.

Mixed layout support

In the e-commerce industry, it is common to see a list of commodities. In the same list, some commodities occupy a whole line, while some lines show 2-3 commodities. This implementation uses GridLayoutManager’s SpanSizeLookup to control how many columns each item occupies.

RecyclerView rv = findViewById(R.id.rv); mAdapter = new SimpleAdapter(null); mAdapter.setLoadMoreView(new CommonLoadMoreView()); mAdapter.setOnLoadMoreListener(this); GridLayoutManager layoutManager = new GridLayoutManager(this, 2); layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) Return position % 3 == 0? Return position % 3 == 0? 2:1; }}); rv.setLayoutManager(layoutManager); rv.setAdapter(mAdapter);Copy the code

RecyclerView supports multiple types

If you want to use the type, when the Adapter is written to inherit PageLoadMultiRecyclerViewAdapter < T, BaseViewHolder >, in which T is a data item type, the type must be implemented IMultiItem interface, And returns the type of the current item in getItemType ()

public class MultiPageLoadAdapter extends PageLoadMultiRecyclerViewAdapter<MultiData, BaseViewHolder> { public MultiPageLoadAdapter(List<MultiData> dataList) { super(dataList); AddItemLayout (multidata.type_text, r.layout.item_simple); addItemLayout(multidata.type_text, r.layout.item_simple); addItemLayout(MultiData.TYPE_IMAGE, R.layout.item_multi_image); addItemLayout(MultiData.TYPE_VIDEO, R.layout.item_multi_video); } @Override protected void convert(BaseViewHolder holder, MultiData item) {// Bind logic switch (holder.getitemviewType ()){case multidata.type_video: holder.setText(R.id.text, item.content); break; case MultiData.TYPE_IMAGE: holder.setText(R.id.text, item.content); break; case MultiData.TYPE_TEXT: holder.setText(R.id.text, item.content); default: break; }}}Copy the code

The introduction method is the same as the above two methods

 RecyclerView recyclerView = findViewById(R.id.rv);
        mAdapter = new MultiPageLoadAdapter(null);
        mAdapter.setLoadMoreView(new CommonLoadMoreView());
        mAdapter.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
Copy the code