Open PatrykAndroid opened 4 years ago
Same issue, do you have any solution?
@wflfei I had a similar problem in the RecyclerView after data update. Solved the problem this way:
var adapter: CustomAdapter
var values: List<Item> = emptyList()
set(value) {
...
adapter.values = values
notifyDataSetChanged()
}
....
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
holder.viewPager.adapter = null // Set null before each update
adapter.notifyDataSetChanged()
holder.viewPager.adapter = adapter
holder.viewPager.setCurrentItem(position, false)
}
I resolved this problem same way like @rusvel. You just need to set your adapter again.
@PatrykAndroid @rusvel Thanks for your reply, I tried set adapter again after call setCurrentItem(position, false), but it didn't perform ok in my case. And I found a way to make it right like this:
image_view_pager?.setCurrentItem(args.pageIndex, false)
//
image_view_pager?.postDelayed({
(image_view_pager?.getChildAt(0) as RecyclerView).scrollBy(-1, 0)
(image_view_pager?.getChildAt(0) as RecyclerView).scrollBy(1, 0)
}, 50)
I also add an issue here: (https://issuetracker.google.com/issues/169090368), Hope it will be fixed sometime.
This worked to me:
val smoothScroll = false
viewPager2.setCurrentItem(index, smoothScroll)
if (!smoothScroll) {
viewPager2.post { viewPager2.requestTransform() }
}
@PatrykAndroid @rusvel Thanks for your reply, I tried set adapter again after call setCurrentItem(position, false), but it didn't perform ok in my case. And I found a way to make it right like this:
image_view_pager?.setCurrentItem(args.pageIndex, false) // image_view_pager?.postDelayed({ (image_view_pager?.getChildAt(0) as RecyclerView).scrollBy(-1, 0) (image_view_pager?.getChildAt(0) as RecyclerView).scrollBy(1, 0) }, 50)
I also add an issue here: (https://issuetracker.google.com/issues/169090368), Hope it will be fixed sometime.
it really work , but why this thing happen?? i face it on update my adapter datas , but Transformer had bad view on the last elements only
viewPager.setCurrentItem(1, smoothScroll = false)
I added my custom PageTransformer and when I am scrolling by finger everything is ok, but when I try to set current item by setCurrentItem(without smooth scroll) method in ViewPager2, PageTransformer doesn't work and items random appear and disappear on my list.