检查RecyclerView是否可滚动

时间:2015-09-23 19:00:33

标签: android android-recyclerview

如何检查RecyclerView是否可滚动,即可见区域下方/上方有项目

我的回收站视图中有一个下拉列表,可以使用notifyItemRangeInserted()notifyItemRangeRemoved()。每当发生任何这种情况时,我想检查RecyclerView是否可滚动,因为我必须调整另一个视图,如报刊亭中的横幅,因此

10 个答案:

答案 0 :(得分:19)

你去了:

public boolean isRecyclerScrollable() {
  LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
  RecyclerView.Adapter adapter = recyclerView.getAdapter();
  if (layoutManager == null || adapter == null) return false;

  return layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}

答案 1 :(得分:18)

我找到了一个简单的解决方案,无论您在列表中的位置如何都可以使用:

public boolean isRecyclerScrollable(RecyclerView recyclerView) {
    return recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth() || recyclerView.computeVerticalScrollRange() > recyclerView.getHeight();
}

答案 2 :(得分:13)

怎么样?
// 1 = down; -1 = up; 0 = up or down
recyclerView.canScrollVertically(0)

答案 3 :(得分:4)

我们的想法是检查最后一个完全可见的元素是否是列表中的最后一个元素。

private boolean isScrollable()
{
    return mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 ==
        mRecyclerViewAdapter.getItemCount();
}

答案 4 :(得分:3)

这个适合我。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

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


            boolean isRecylerScrollable = recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth();

            if(isRecylerScrollable){
             //do something
            }
    });
}

答案 5 :(得分:2)

Kotlin ,你可以这样做:

 fun isRecyclerScrollable(): Boolean {
    val layoutManager = recyclerView.layoutManager as LinearLayoutManager
    val adapter = recyclerView.adapter
    return if (adapter == null) false else layoutManager.findLastCompletelyVisibleItemPosition() < adapter.itemCount - 1
}

答案 6 :(得分:0)

boolean isScrollable() {
    LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    int itemCount = mRecyclerView.getAdapter().getItemCount();

    return !(itemCount == 0 ||
    (layoutManager.findFirstCompletelyVisibleItemPosition() == 0 &&
    layoutManager.findLastCompletelyVisibleItemPosition() == itemCount - 1));
}

答案 7 :(得分:0)

我遇到了同样的问题,已经发布的答案(尤其是Fabios)使我步入正轨。但是scrollRange不支持填充,因此,如果您的Recyclerview有填充,则也需要考虑填充。

我现在在Kotlin中使用以下扩展属性来确定视图是否在滚动。

/**
 * computes whether the RecyclerView's content is larger than the view container, i.e. whether or not the user can scroll
 * returns null if either layout manager or adapter is not yet set or an incompatible layout manager is used (Linear- & GridLayoutManager work)
 */
val RecyclerView.canScroll: Boolean?
    get() {
        // if there's no adapter set, we don't know yet whether or not the view is scrollable
        if (adapter == null) return null
        val manager = layoutManager as? LinearLayoutManager ?: return null
        if (manager.orientation == RecyclerView.HORIZONTAL) {
            return computeHorizontalScrollRange() + paddingLeft + paddingRight > width
        }
        return computeVerticalScrollRange() + paddingTop + paddingBottom > height
    }

答案 8 :(得分:0)

使用这种方法:

recyclerView.viewTreeObserver.addOnScrollChangedListener {
    val canScrollUp = recyclerView.canScrollVertically(-1)
    val canScrollDown = recyclerView.canScrollVertically(1)
}

答案 9 :(得分:0)

  1. 首先创建一个布尔值来检查循环。
  2. 添加 onScrolled 侦听器:
binding.rvInformation.addOnScrollListener(object : RecyclerView.OnScrollListener() {
           override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
               super.onScrolled(recyclerView, dx, dy)
               if (!scrollChecked) { // <- check the 1 loop
                   scrollChecked = true
                   if (binding.rvInformation.canScrollVertically(1)) {
                       // Can scroll
                   } else {
                       // Can't scroll
                   }
               }

           }
       })