Skip to content Skip to sidebar Skip to footer

Can't Find Recyclerview Visible Item Position Inside Nestedscrollview

How do I get the first/ last completely visible item in a recyclerview if it is inside a NestedScrollView and the recycler has nestedScrollingEnabled='false' for smooth scrolling w

Solution 1:

You can add scroll listner on nestedscrollview and while scrolling check each item in recylerview that is placed inside the nestedScrollview that is visible or not. Below is the example

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

  nestedScrollView?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
         overridefunonScrolled(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 in0 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")
                                }
                    }
               }
          }
      }
  })

Solution 2:

Well I couldn't find any way in which two RecyclerView could be inside a NestedScrollView and still say which item is visible on the screen.

So my solution was to remove the NestedScrollView and make the Vertical RecycelerView the main view in the page and add the Horizontal RecyclerView as the first item of on Vertical Recycler. As I only wanted to know the position for the Vertical Recycler, I can get that now by using any of the methods mentioned in the question.

Solution 3:

When we add RecylerView in a NestedScrollView, while scrolling the function findLastVisibleItemPosition always return the last item in the list.

So my solution is instead of trying find the last visible item of RecyclerView. I added OnScrollChangeListener to NestedScrollView for detecting the last item.

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

Post a Comment for "Can't Find Recyclerview Visible Item Position Inside Nestedscrollview"