【问题标题】:How to check whether scrollview / recyclerview is scrolling to up or down android如何检查scrollview / recyclerview是否向上或向下滚动android
【发布时间】:2016-12-08 18:32:35
【问题描述】:

我正在开发 android 应用程序并在某些活动中使用 recyclerview 和 scrollview。现在我想在 recyclerview/scrollview 向上滚动时显示一些布局,并在向下滚动时隐藏布局。 我想要一个可以告诉 recyclerview/scrollview 向上或向下滚动的函数。

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

非常感谢。

【问题讨论】:

标签: android android-recyclerview android-scrollview


【解决方案1】:
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;

}

【讨论】:

  • 如果 currentFirstVisible == firstVisibleInListview 和 out 项目高度为 200dp。
【解决方案2】:

这个问题的简短答案

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 {

                    }


        }
    });

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多