Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager setCurrentItem Slide

I have an Activity contains ViewPager , first Fragment in this ViewPager contains ListView and when I click on any Item it replaces this Fragment with selected one , I use setCurrentItem(selectedItem,true)//for smooth scroll but ViewPager slides over all Fragments before selected Fragment I want it to slide to selected Fragment only not over all fragments I tried to use PageTransfrom.transformPage like that

@Override
public void transformPage(View page, float position) {
    int currentItem = viewPager.getCurrentItem();
    if (currentItem != selectedItem)
        page.setVisibility(View.GONE);
}

but with no luck

like image 249
Ma7moud El-Naggar Avatar asked May 03 '15 15:05

Ma7moud El-Naggar


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

What is ViewPager used for?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

What is ViewPager offscreen page limit?

Is it possible to disable the offscreen page limit? No. It is already set to the minimum possible value: one page to each side of the viewed page. This is necessary to have the animation effects work -- you see parts of two fragments (original and new) at the same time.

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.


1 Answers

One possible solution is to modify the PagerAdapter to always return the page you want on item #2.

When clicked on any item from the first page, first change the implementation of the method getItem(int position) of the PagerAdapter, (this is easy if your PagerAdapter is implemented in the same class file of your code), to return the page you want when position == 2, and then call setCurrentItem(2, true). This could make a smooth scroll to the selected page, directly. You may also need to change getCount() to limit the count to only 2, otherwise the next page may not be the expected one.

But this method dismisses the other pages. User could only scroll back to the first page and then navigate to another page. If you want keep the natural order of the pages by manual scroll (not by click), you could restore getItem(int position) to your default implementation once the scoll (by click) stops, and reload the stopped current page to the natural order by calling setCurrentItem(position, false)(immediately, not smoothly this time), then user can scroll back and forth manually.

like image 55
HC Zhang Avatar answered Nov 06 '22 23:11

HC Zhang