【问题标题】:Can't find RecyclerView visible item position inside NestedScrollView在 NestedScrollView 中找不到 RecyclerView 可见项目位置
【发布时间】:2017-04-12 13:05:41
【问题描述】:

如果它位于 NestedScrollView 内并且回收器有 nestedScrollingEnabled="false" 用于与 RecyclerView 上方的其他视图平滑滚动,我如何在 recyclerview 中获取第一个/最后一个完全可见的项目。

所有这些功能

int findFirstVisibleItemPosition(); int findFirstCompletelyVisibleItemPosition(); int findLastVisibleItemPosition(); int findLastCompletelyVisibleItemPosition();

返回在 recyclerView 中创建的第一个/最后一个项目。

我想找到当前可见的项目,因为我想让 RecyclerView 无限滚动,如果只有几个项目可以滚动,我应该获取数据。

谢谢

【问题讨论】:

  • 我也有同样的问题。但我找不到我的答案。 :(
  • @BuiMinhDuc 所以实际上我们没有办法做到这一点。最好的选择是做我在答案中写的。
  • 另外,getChildCount() 总是返回与 getItemCount() 相同的结果。

标签: android android-recyclerview


【解决方案1】:

您可以在 nestedscrollview 上添加滚动侦听器,并在滚动时检查 recylerview 中放置在可见与否的 nestedScrollview 内的每个项目。 下面是例子

  val scrollBounds = Rect()
  nestedScrollView?.getHitRect(scrollBounds)

  nestedScrollView?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
         override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val scrollBounds = Rect()
                if (recyclerView != null && recyclerView.adapter != null
                            && recyclerView.adapter?.itemCount != null
                            && recyclerView.adapter?.itemCount!! > 0) {
                     for (i in 0 until recyclerView.adapter?.itemCount!!) {
                         var view = recyclerView?.findViewHolderForAdapterPosition(i)?.itemView

                         if (view != null) {
                            if (view.getLocalVisibleRect(scrollBounds)) {
                               if (!view.getLocalVisibleRect(scrollBounds)
                                            || scrollBounds.width() < view.getWidth()) {
                                        Logger.debugLog("WidgetScrolled", "BTN APPEAR PARCIALY")
                                    } else {
                                        Logger.debugLog("WidgetScrolled", "BTN APPEAR FULLY!!!")
                                    }
                                } else {
                                    Logger.debugLog("WidgetScrolled", "No")
                                }
                    }
               }
          }
      }
  })

【讨论】:

  • 在嵌套的 recyclerview 中工作得很好。谢谢一百万。
【解决方案2】:

嗯,我找不到任何方法可以让两个RecyclerView 位于NestedScrollView 中,并且仍然可以说哪个项目在屏幕上可见。

所以我的解决方案是删除NestedScrollView 并使Vertical RecyclerView 成为页面中的主视图,并将Horizo​​ntal RecyclerView 添加为Vertical Recycler 的第一项。由于我只想知道 Vertical Recycler 的位置,我现在可以通过使用问题中提到的任何方法来获得它。

【讨论】:

  • 检查这个answer,答案解释了为什么这些方法不起作用。
  • 有一个解决方案是你应该在 NestedScrollView 中为 RecyclerView 视图设置高度
【解决方案3】:

当我们在 NestedScrollView 中添加 RecylerView 时,在滚动时 findLastVisibleItemPosition 函数总是返回列表中的最后一项。

所以我的解决方案不是尝试找到 RecyclerView 的最后一个可见项。我将 OnScrollChangeListener 添加到 NestedScrollView 以检测最后一项。

scvHome.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            val isLoadingMore = homeViewModel.isLoadingMoreProduct.value ?: false
            if (!isLoadingMore) {
                v?.let {
                    val currentHeight = (v?.getChildAt(0).measuredHeight - v.measuredHeight)
                    if (scrollY == currentHeight && hasMoreProduct()) {
                        homeViewModel.loadMoreProduct()
                    }
                }
            }
        })

【讨论】:

    【解决方案4】:

    我知道为时已晚。但我得到了解决方案。 (Java 代码)

    所以我的解决方案不是尝试找到 RecyclerView 的最后一个可见项。我将 OnScrollChangeListener 添加到 NestedScrollView 以检测最后一项。

    nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                        if (v.getChildAt(v.getChildCount() - 1) != null) {
                            if (scrollY > oldScrollY) {
                                if (scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) {
                                    //code to fetch more data for endless scrolling
                                  
                                }
                            }
                        }
        
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2016-09-01
      相关资源
      最近更新 更多