3

This is the use case:

I have a recyclerview list of 5 food stuff imageviews:

  1. Watermelon
  2. Mango
  3. Peach
  4. Apple
  5. Guava

I want to log an output (Log.e(TAG, "Peach shown!") when the peach imageview is fully visiile in the recyclerview when the user scroll past it.

Is there a callback that is called when a view is fully visible in the recyclerview? I almost want to turn the method findFirstCompletelyVisibleItemPosition () into a callback method: http://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#findFirstCompletelyVisibleItemPosition()

I read this thread: RecyclerView - callback when view is no longer visible and looked at the docs here http://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnChildAttachStateChangeListener.html but I see the callback method onChildViewAttachedToWindow() is called when:

Called when a view is attached to the RecyclerView.

Attached can be before the view actually gets displayed.

I can think of one highly inefficient way of doing this:

  1. Set the RecyclerView.OnScrollListener on your recyclerview

  2. Call mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() inside the onScrolled method. Check the int that is returned to equal 3.

Each time the recyclerview scrolls, hundreds of calculations are going to be performed.

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217

1 Answers1

4

You actually should handle it in the onScrolled() method, as you mentioned. The findFirstCompletelyVisibleItemPosition() method is very efficient and the calculations being made will not affect most devices, including the low-end ones.

As a side note, I think what you want is the findLastCompletelyVisibleItemPosition() method.

Been there, done that :-)

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • OK.let me try that later today. – Simon Nov 10 '15 at 14:37
  • I tried this method but it calls the log a few times when it scrolls pass the peach imageview. I just want it to be called once. Do you know how I can make it call once only? – Simon Nov 11 '15 at 17:07
  • There are several patterns for that, depending on what you want to achieve. Basically, you need to trigger a "lock condition" after getting the right CompletelyVisibleItem and then reset it on some other condition (change in list element count, getting to the end of the list, exiting the Activity/Fragment). But again, this is very implementation-specific and I can not give you a single solution without knowing what your exact goal is :-) – Kelevandos Nov 11 '15 at 19:15
  • I used RxJava in order to make it trigger once. Create an observable that registers a scroll listener, gets the first and last visible position on each scroll, returns a position range. use `scan()` to keep track of the current and previous range. Then finally, check if your target is in the current range, but not the previous one. `take(1)` of those and it will complete and clean up after the first match. – Avi Cherry Sep 01 '17 at 17:42