How can I identify that RecyclerView's last item is visible on screen?

前端 未结 5 1958
自闭症患者
自闭症患者 2021-01-31 19:18

I have one RecyclerView and I added list of data into the RecyclerView. I wanted to add more data in list, when last RecyclerView item is

5条回答
  •  没有蜡笔的小新
    2021-01-31 20:02

    Seen many of the above answers but my answer is different one and it will work in your cases also. My approach is based on scroll state of recylerview. Maintain below variable "check" and this should update only once when api responds. Put below code in your api response. If you want to handle last item only on every call of api.

    final boolean[] check = {true};
        recyclerView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (!recyclerView.canScrollVertically(1)) {
                    // last item of recylerview reached.
                    if (check[0]) {
                        //your code for last reached item
                        scroll_handler.setVisibility(View.GONE);
                    }
                } else {
                    scroll_handler.setVisibility(View.VISIBLE);
                    check[0] = false;
                }
            }
        });
    

    If you want to handle your last item every time then do it as below

    recyclerView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (!recyclerView.canScrollVertically(1)) 
                // Bottom of recyler view.
                arrow_img.setRotation(180);
            }
        }
    });
    

提交回复
热议问题