Dd Mm
Dd Mm

Reputation: 11

Disable Smooth scroll when jump current item to another item in viewpager2 android

Hey everyone I am new in android with kotlin. I have problem in viewpager2 which I cannot solve. I have one Mutable list in which 30 items data is present on it. When application opens I sat by default 15 index number data as default as index of viewpager2 as current item. If I move to page to 0 index then, I am reseting the Mutablelist data and adding 10 or 20 data on left and 0 index value set in the middle, which means I am shifting the data in which I am in successful. But the main problem is to disable the smooth scroll between 0 to 10-20 when I reset the list also I have observable variable as my current index which I am changing the current item of viewpager2.

private var b = false

 mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
    override fun onPageScrolled(int position, float arg1, int arg2) {
            if(b){
                 if(position > 0 && position < 15){
                    mViewPager.setCurrentItem(currenIndex,false)
                    b = false
                 }
             }
    }
}

In Activity when I am resetting the mutable list or adding some value on it I changed the boolean value as true

 b = true

also in my observable code

currenIndex.observer(this, Observer {
 mViewPager.setCurrentItem(it, true)
})

It always jumping when resetting the list to 0,1,2,3,4,5,6,7,8,9,10.. and I want to disable all animation between them and only wanted to display the page directly 15 page

Also I tried to remove

mViewPager.setCurrentItem(it, true)

from my observable variable it works but their is some problem in between 0 and 1 the animation stops between them when first time to reaching index 0 without resetting the list.

Sorry for my English. Thanks in advance

Upvotes: 1

Views: 1509

Answers (1)

Wilson Tran
Wilson Tran

Reputation: 4811

The second parameter of ViewPager.setCurrentItem funtion is boolean smoothScroll Try this:

mViewPager.setCurrentItem(15, false)

Upvotes: 3

Related Questions