I am using local database to load records on recyclerview the problem is the size of records its about six thousand and when I click to open or show those records in recyclerview my device stuck for a few seconds a blank screen appear and when few seconds pass it appear and show records. Is there a way to load thousands of records or data on recyclerview without blockage of UI? please let me know thanks
Asked
Active
Viewed 454 times
0
-
use paging library https://developer.android.com/reference/android/arch/paging/LivePagedListBuilder.html – Pentiux Jul 24 '21 at 09:42
1 Answers
1
copy & paste this method to your Activity / Baseactivity. and call this after yourRecyclerview.setAdapter(YourAdapter)
public void PagginationRecyclerView(RecyclerView rv) {
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int total = layoutManager.getItemCount();
int currentLastItem = layoutManager.findLastVisibleItemPosition();
if (currentLastItem == total - 1) {
Toast.makeText(BaseActivity.this, "Load Next", Toast.LENGTH_SHORT).show();
// Request to Database to load more data
}
}
});
}

Muhammad Ali
- 1,055
- 6
- 11
-
this method will reload more data from db only when scroll reach to the last -1 visible item in your recyclerview. (you've to modify & replace the toast message). there is another library also available called, SwipeToRefresh you can find this on github. please appriciate by up-voting if it helped you. – Muhammad Ali Jul 24 '21 at 11:22
-
could you please also share sqlite query to load limited number of records at a time ! – Ali Jul 24 '21 at 11:25
-
Better to Load the limited data (suppose 50 row or entries from db) and show. Than again load next 50 entries. ⚠️ loading whole data will impact on the speed of your app and will create memory issue too. – Muhammad Ali Jul 24 '21 at 17:04
-
I wish i could share the source code for your problem but unfortunately i don’t have now. – Muhammad Ali Jul 24 '21 at 17:06