I'm using RecyclerView on my Android App. I want to show firstly bottom and scroll from bottom to top. I can did it but I want to add Scroll Listener and when the scroll position on top I call a function.
chatRecycler = (RecyclerView) findViewById(R.id.chatRecycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
chatRecycler.setLayoutManager(layoutManager);
chatRecycler.setItemAnimator(new DefaultItemAnimator());
And I try that but it's not working. This code call the function to bottom:
chatRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
pageId++;
chatDuoPresenter.getChatData(pageId);
}
}
});
Edit: I'm still searching solution.
Solution:
@Override
public void initScrollListener() {
chatRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0) {
pageId++;
chatDuoPresenter.getChatData(pageId);
}
}
});
}