如何检查scrollview / recyclerview是否向上或向下滚动到android

时间:2016-08-03 07:12:32

标签: android android-recyclerview android-scrollview

我正在开发Android app并在某些活动中使用recyclelerview和scrollview。现在,我想在recyclelerview / scrollview向上滚动时显示一些布局,并在向下滚动时隐藏布局。 我想要一个能够告诉recyclelerview / scrollview向上或向下滚动的函数。

如果有人知道如何为recyclelerview和scrollview执行此操作,请提供帮助。

非常感谢先进。

2 个答案:

答案 0 :(得分:2)

Try this way:

private static int firstVisibleInListview;

firstVisibleInListview = yourLayoutManager.findFirstVisibleItemPosition();

In your scroll listener:

 public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{
 super.onScrolled(recyclerView, dx, dy);

 int currentFirstVisible = yourLayoutManager.findFirstVisibleItemPosition();

 if(currentFirstVisible > firstVisibleInListview)
   Log.i("RecyclerView scrolled: ", "scroll up!");
 else
   Log.i("RecyclerView scrolled: ", "scroll down!");  

 firstVisibleInListview = currentFirstVisible;

}

答案 1 :(得分:0)

  

这个问题的简短答案

scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {


                    int x = scrollY - oldScrollY;
                    if (x > 0) {
                        //scroll up
                    } else if (x < 0) {
                       //scroll down
                    } else {

                    }


        }
    });