12

I have implemented a recyclerview with some videos (say 10). Clicking on any item opens a viewpager2 which is on some other activity with all the items and the clicked one (from recyclerview) is shown. After that user can scroll to other item from there only. Everything is working fine.

Only the problem is with the scrolling when recyclerview item click where is the viewpager2 is opened.

Whenever any item is selected(10th). The viewpager opens first and the scroll itself to the (10th) position. I want to open 10th position of viewpager2 directly without scrolling. Any help would be appreciated.

I have tried setting the current item as

viewPagerReview.currentItem = recyclerviewItemSelectedPosition

but if the current item is last position of the recyclerview then viewpager also scrolls till last position.

Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
  • Can you please share more of your code please? The problem seems pretty clear; however, it would be much more helpful if you could. There's a method in the `RecyclerView` that scrolls without _smooth scrolling_. You wouldn't see it scroll, it would directly take you to the position you wish. – Nizar Jan 20 '20 at 16:24
  • Have you tried `recyclerView.layoutManager.scrollToPosition`? – Nizar Jan 20 '20 at 16:25
  • @Nizar ok, I think my question bit confusing here. Issue is with viewpager2 scroll. I am not scrolling the recyclerview here. need to open viewpager which is on some other activity with the position selected on recycler list. Thanks for responding quickly. I have edited my question. – Narendra Pal Jan 20 '20 at 16:29
  • Actually, you were clear, I wasn't. According to the AndroidWiki `viewpager2` has a default `smoothScroll` when you set `currentItem`. Now my question is, would you consider passing the `selectedPosition` and scrolling to it with the `RecyclerView` or no? – Nizar Jan 20 '20 at 16:34
  • no, not with the recycler view. just use the position and the list data to viewpager2 directly. Suppose, if the recycler view have 15 items and I click on 10th then viewpager gets opened in another activity and scrolls to the 10th position due to `viewPagerReview.currentItem = recyclerviewItemSelectedPosition` code. – Narendra Pal Jan 20 '20 at 16:39
  • Yes, my bad, I should have checked the docs before answering. Turns out `viewpager2` does have a method to disable the smooth scrolling. – Nizar Jan 20 '20 at 16:42
  • recyclerView.layoutManager.scrollToPosition and setting the transition/animation to null is the right way to do it. – Faizan Tariq Jan 20 '20 at 16:49
  • @FaizanTariq how to set transition/animation to null? – Narendra Pal Jan 20 '20 at 17:01
  • simply set smooth scroll of viewPager to false, it will disable all transition animations for that viewpager. – Faizan Tariq Jan 20 '20 at 17:13

2 Answers2

30

Alright, according to ViewPager2 on the AndroidDocs, this is what setCurrentItem(item: Int) would do:

Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.

Therefore, you're gonna want to use the other method, which is setCurrentItem(item: Int, smoothScroll: Boolean) in order to specify whether you want to smoothScroll or not.

So, in your case, go for:

viewPagerReview.setCurrentItem(recyclerviewItemSelectedPosition, false)
Nizar
  • 2,034
  • 1
  • 20
  • 24
  • I'm glad it did @NarendraPal :) – Nizar Jan 20 '20 at 16:46
  • 4
    But this actually does not work reliably. Sometimes it selects the proper page, sometimes not. Probably a race condition in the event driven Android mess. I had to delay it a little bit using the postDelayed() but please note that it is a nasty hack, which does not guarantee it will work on every device :( – Jiří Křivánek Apr 22 '20 at 18:54
  • 1
    @JiříKřivánek this isn't a hack, this is an actual proper method used. Now the race condition I am not aware. There's a chance that that method might interfere with something else causing this race condition. Is there any way you can share with me a project or some code or a github repo where this happens? If you can, please do... I'd love to see exactly the issue you are facing. – Nizar Apr 22 '20 at 19:11
  • 2
    It wasn't working for me until I updated to `"androidx.viewpager2:viewpager2:1.1.0-alpha01"` – patrick-fitzgerald Sep 19 '20 at 00:33
  • 1
    Same here. Its was reproducably not working at first call. switching to 1.1.0-alpha01 helps – Flo rian May 25 '21 at 13:10
0

The correct solution for this problem follow this approach setCurrentItem(item :Int, smoothScroll : Boolean) viewPager.setCurrentItem( 2, false)