RecyclerView scrolled UP/DOWN listener

前端 未结 5 524
无人共我
无人共我 2020-11-29 01:32

How do we know if user scrolled down or up in RecyclerView ?

I tried with RecyclerView#OnScrollListener , it gives the amount of vertical s

5条回答
  •  庸人自扰
    2020-11-29 02:07

    I wanted to hide a layout if the recyclerview is scrolled down and then make it visible if the recyclerview is scrolled up. I did some thinking and came up with this logic. Variable y is a global static int. Do not forget to declare y as static int y;

    I hope it helps someone :)

     mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(lLayout) {
               @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                // super.onScrolled(recyclerView, dx, dy);
                    y=dy;
                }
    
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if(mRecyclerView.SCROLL_STATE_DRAGGING==newState){
                        //fragProductLl.setVisibility(View.GONE);
                    }
                    if(mRecyclerView.SCROLL_STATE_IDLE==newState){
                       // fragProductLl.setVisibility(View.VISIBLE);
                        if(y<=0){
                            fragProductLl.setVisibility(View.VISIBLE);
                        }
                        else{
                            y=0;
                            fragProductLl.setVisibility(View.GONE);
                        }
                    }
                }
            });
    

提交回复
热议问题