'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 with other views above the RecyclerView.

All these functions

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

either return the first/last item created in the recyclerView.

I want to find the current visible item because I want to make the RecyclerView scroll infinitely and I should fetch data if only a few items are left to scroll.

Thanks



Solution 1:[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() {
         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")
                                }
                    }
               }
          }
      }
  })

Solution 2:[2]

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 ->
            val isLoadingMore = homeViewModel.isLoadingMoreProduct.value ?: false
            if (!isLoadingMore) {
                v?.let {
                    val currentHeight = (v?.getChildAt(0).measuredHeight - v.measuredHeight)
                    if (scrollY == currentHeight && hasMoreProduct()) {
                        homeViewModel.loadMoreProduct()
                    }
                }
            }
        })

Solution 3:[3]

I know its too late. But i got solution. (Java code)

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

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
                              
                            }
                        }
                    }
    
                }
            });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 NhatVM
Solution 3 Gayathri