Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 230
Akash Popat Avatar asked Apr 12 '17 13:04

Akash Popat


3 Answers

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")
                                }
                    }
               }
          }
      }
  })
like image 134
Muhammad Noman Avatar answered Oct 20 '22 04:10

Muhammad Noman


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.

like image 28
Akash Popat Avatar answered Oct 20 '22 04:10

Akash Popat


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
                              
                            }
                        }
                    }
    
                }
            });
like image 1
Gayathri Avatar answered Oct 20 '22 03:10

Gayathri