An example of how-to implement an infinite scrolling adapter for a RecyclerView, with a ProgressBar footer. Blog post can be found here: http://msobhy.me/2015/09/05/infinite_scrolling_recyclerview/
public class MyActivity extends ActionBarActivity { | |
@InjectView(R.id.myRecyclerView) | |
RecyclerView mRecyclerView; | |
RecyclerViewFooterAdapterImpl mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(getLayoutResource()); | |
mRecyclerView.setHasFixedSize(true); | |
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); | |
mRecyclerView.setLayoutManager(mLayoutManager); | |
mAdapter = new RecyclerViewFooterAdapterImpl(mRecyclerView, vehicleModelEngines, new OnLoadMoreListener() { | |
@Override | |
public void onLoadMore() { | |
callEndpointForPage(vehicleMakeModel.getId(), new Runnable() { | |
@Override | |
public void run() { | |
mAdapter.removeItem(null); // don't forget to remove the progress bar representative value | |
updateAdapter(false); | |
} | |
}); | |
} | |
}, this, currentSortType); | |
mRecyclerView.setAdapter(mAdapter); | |
} | |
private void updateAdapter(boolean shouldGoUp) { | |
List<MyModelType> myDataSet = new ArrayList<>(MyModelType); | |
mAdapter.resetItems(myDataSet, currentSortType); | |
if (shouldGoUp) { | |
if (mAdapter.getFirstVisibleItem() <= 50) { | |
mRecyclerView.smoothScrollToPosition(0); | |
} else { | |
mRecyclerView.scrollToPosition(0); | |
} | |
} | |
} | |
} |
<ProgressBar | |
android:id="@+id/progressBar" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:padding="@dimen/space_medium"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
longph92 commentedApr 4, 2016
totalItemCount = linearLayoutManager.getItemCount();
This line always returned is 0. Why?? Help me please!