yarolegovich / DiscreteScrollView

A scrollable list of items that centers the current element and provides easy-to-use APIs for cool item animations.
5.77k stars 779 forks source link

Accessing Dataset in onCurrentItemChanged #86

Closed mrahimygk closed 6 years ago

mrahimygk commented 6 years ago

I want to access the data model of the currently showing carousel item. I could not find this in the docs. tried to log the two arguments to see if they look like an index, but they are not indices.

recyclerview?.addOnItemChangedListener { viewHolder, adapterPosition ->
            Log.d(TAG, "item changed viewHolder " + viewHolder)
            Log.d(TAG, "item changed viewHolder adapterPosition " + viewHolder?.adapterPosition)
            Log.d(TAG, "item changed viewHolder position  " + viewHolder?.position)
            Log.d(TAG, "item changed adapterPosition " + adapterPosition)

Do I have to get the index by calling a method from the library? checking SO didn't lead to any usable solution. I think there is no way to access the currently showing item.

yarolegovich commented 6 years ago

adapterPosition is what you're looking for. Do you use InfiniteScrollAdapter?

yarolegovich commented 6 years ago

Anyway, just put some effort and check how this was done in the sample app.

mrahimygk commented 6 years ago

yes. with InfiniteScrollAdapter , adaperPosition is an integer much bigger than the actual length of my dataset array. Do I need to perform the modulo arithmetic? This gives me the wrong index:

adapterPosition % provinceAdapter!!.itemCount

yarolegovich commented 6 years ago

Have you tried to read the docs or to look at the sample code?

mrahimygk commented 6 years ago

Those are descriptive method names, Thanks to mention; so I know that what method I should be using. The problem is that sometimes cityInfiniteScrollAdapter!!.getRealPosition(adapterPosition) gives me the wrong index. I think the method skips the last item in dataset.

yarolegovich commented 6 years ago

I doubt it. The sample works fine and no one has ever reported this issue before.