2

by default ViewPager swipe direction is left to right to view other tabs with content, now i like to change this direction to right to left.

my FragmentPagerAdapter class :

public static class MyPagerAdapter extends FragmentPagerAdapter {
    private static int NUM_ITEMS = 3;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return MainCategoryListFragment.newInstance(2);
            case 1:
                return ChildFragment.newInstance(1);
            case 2:
                return ParentFragment.newInstance(0);
            default:
                return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        String title = "";
        switch (position){
            case 0:
                title =  "one";
                break;
            case 1:
                title =  "two";
                break;
            case 2:
                title =  "tree";
                break;
        }
        return title;
    }
}
Mochamad Taufik Hidayat
  • 1,264
  • 3
  • 21
  • 32

2 Answers2

0

ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);

pageIndex is total page count. by doing this user will be forced to swipe from right to left.

manojprasad
  • 158
  • 2
  • 8
0

Copy-pasted answer from here

I faced the same problem month ago but I think I found a great solution.

1) Change your TabLayout direction to ltr in xml:

<android.support.design.widget.TabLayout
    android:id="@+id/tl_activity_main"
    android:layout_below="@id/toolbar_activity_main"
    android:layoutDirection="ltr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/DisionTabLayout" />

2) Create custom adapter for ViewPager and override methods getItem(int position) and getPageTitle(int position):

@Override
public Fragment getItem(int position) {
    if (mIsRtlOrientation && mTabs != null && mTabs.length > 0) {
        return mTabs[mTabs.length - position - 1].getFragment();
    } else {
        return mTabs[position].getFragment();
    }
}

@Override
public int getCount() {
    return mTabs.length;
}

@Override
public CharSequence getPageTitle(int position) {
    if (mIsRtlOrientation && mTabs != null && mTabs.length > 0) {
        return mTabs[mTabs.length - position - 1].getTitle();
    } else {
        return mTabs[position].getTitle();
    }
}

3) Set the adapter to ViewPager and then apply this ViewPager to TabLayout.

private void setAdapters() {
    // initialize adapter
    mTabsAdapter = new TabsAdapter(getSupportFragmentManager(), mTabs, true);
    // set adapter to ViewPager
    vp.setAdapter(mTabsAdapter);
    // set ViewPager to TabLayout
    tl.setupWithViewPager(vp);
    // if RTL orientation, go to last item
    vp.setCurrentItem(vp.getAdapter().getCount() - 1, false);
}

4) I created a blog post which contains more details and full code - here