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);
}
});
....
}
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.
It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.
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.
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
}
}
The logic of infinite scroll can be in the adaptor. Like that:
You can load next elements before last element are showed too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With