-
Notifications
You must be signed in to change notification settings - Fork 1
Application
- PaginatedStubRecyclerAdapter
- MainActivity
- StubItemVO
###PaginatedStubRecyclerAdapter public class PaginatedStubRecyclerAdapter extends RecyclerView.Adapter<PaginatedStubRecyclerAdapter.ViewHolder>
PaginatedStubRecyclerAdapter is a sample Adapter for RecyclerView. It’s main feature is possibility to handle pagination.
It can:
- enable data load
- disable data load
- load next page (preloading page offset)
The following methods are used to load next page
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position > mItemList.size() - PRE_LOADING_PAGE_OFFSET) {
loadNextPage();
}
StubItemVO itemVO = mItemList.get(position);
holder.imageView.setImageWithURL(itemVO.getImageURL());
holder.textView.setText(itemVO.getDescription());
}
@Override
public int getItemCount() {
return mItemList.size();
}
private void loadNextPage() {
if (mCanLoadMore && !mOnLoad) {
mOnLoad = true;
String pageId = Integer.toString(mPagesLoaded);
Model.instance().getStubManager().pullPage(pageId);
}
}
public void enableDataLoad() {
Model.instance().getStubManager().addDataFetchCompleteListener(listener);
loadNextPage();
}
public void disableDataLoad() {
Model.instance().getStubManager().removeDataFetchCompleteListener(listener);
}
Figure below shows next page load shema.
###MainActivity public final class MainActivity extends BaseActivity
SimpleListActivity consists of CoordinatorLayout with wrap RecyclerView. This class enables data loading in onCreate method and disables data loading in onDestroy method.
Here is an example of the MainActivity that contains template of usage of adapter with pagination:
private StubItemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new StubItemAdapter(this);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
adapter.enableDataLoad();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(adapter != null)
{
adapter.disableDataLoad();
}
}
###StubItemVO public class StubItemVO extends AbstractVO
This class contains data that will be saved in the database and shown in the user interface.