简体   繁体   中英

RecyclerView onScrollListener -on Reverse Layout-

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

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