androidandroid-recyclerviewpaginationandroid-roomandroid-threading

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


Solution

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