Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement infinite scroll in Gridlayout recylcerview

I need help with the onscroll method...So Iwe tried a couple of things I found on stackoverflow but most of them work for linearlayout etc. Would be mighty helpful if you could point me in the right direction...

Im using the instagram api to load pictures. it sends 20 pictures at a time so i need to load more as i get to the bottom of the page

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        //configViews();
//        mLayoutManager = new GridLayoutManager(this, 2);
//        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {


            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);



            }
        });

....
}
like image 320
Hasan Nagaria Avatar asked Feb 27 '16 18:02

Hasan Nagaria


People also ask

How is infinite scroll implemented?

Infinite scrolling will require two key parts. One part will be a check for the window scroll position and the height of the window to determine if a user has reached the bottom of the page. Another part will be handling the request for additional information to display.

How can show all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

Does RecyclerView automatically scroll?

To make RecyclerView auto scroll, we have to call smoothScrollToPosition every specified interval of time. We can use Flowable. interval to emit sequential Long value. To stop auto scroll if a user swipes RecyclerView, we can listen to the scroll state change.


2 Answers

Add this in OnScrolled.This is the basic idea.You can modify it as per your requirement

if(dy > 0){ // only when scrolling up

    final int visibleThreshold = 2;

    GridLayoutManager layoutManager = (GridLayoutManager)recyclerview.getLayoutManager();
    int lastItem  = layoutManager.findLastCompletelyVisibleItemPosition();
    int currentTotalCount = layoutManager.getItemCount();

    if(currentTotalCount <= lastItem + visibleThreshold){
        //show your loading view 
        // load content in background

    }
}
like image 139
Ravi Theja Avatar answered Oct 01 '22 02:10

Ravi Theja


The logic of infinite scroll can be in the adaptor. Like that:

  • Return size + 1 in your adaptor (for loading view)
  • When the last position are asked, return a view that indicate loading
  • Run a background task that will download next elements and change data in the adaptor

You can load next elements before last element are showed too.

like image 26
Kevin Robatel Avatar answered Oct 01 '22 01:10

Kevin Robatel