简体   繁体   中英

How to load thousands of records from local database to recyclerview without blocking UI using ROOM library

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

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
            }
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM