user3718930
user3718930

Reputation: 381

PagerSlidingTabStrip- how to right to left tab

I am using PagerSlidingTabStrip library to make my tabs . I can't swap it right to left.

I've searched and the solution was to

add tabsContainer.setGravity(Gravity.RIGHT);

but it didn't work for me .

I changed the library code to this but it didn't work :

tabsContainer = new LinearLayout(context);
    tabsContainer.setGravity(Gravity.RIGHT);
    tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
    tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    addView(tabsContainer);

How can I right to left the tabs?

Upvotes: 1

Views: 1267

Answers (2)

EAGLE4YE
EAGLE4YE

Reputation: 346

add: android:layoutDirection="ltr" to PagerSlidingTabStrip layout

Upvotes: 0

Hamid Zandi
Hamid Zandi

Reputation: 2902

if you use this library

https://github.com/astuetz/PagerSlidingTabStrip

add this line in PagerSlidingTabStrip before line 164 :

pager.setCurrentItem(pager.getAdapter().getCount()-1);

like this :

public void setViewPager(ViewPager pager) {
    this.pager = pager;

    if (pager.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }

    pager.setOnPageChangeListener(pageListener);
    pager.setCurrentItem(pager.getAdapter().getCount()-1);
    notifyDataSetChanged();
}

and in your activity reverse fragment sort position like this:

public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
            "Top New Free", "Trending" };

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

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

    boolean enableStickerToSticker = true;
    boolean enableCommentSticker = true;
    boolean enableLatestTap = true;

    @Override
    public Fragment getItem(int position) {
            return LatestListFragment.newInstance(TITLES.length - 1
                    - position);
    }

}

but if you use this library :

https://github.com/jpardogo/PagerSlidingTabStrip

change this line in activity :

   pager.setCurrentItem(1);

to this:

   pager.setCurrentItem(pager.getAdapter().getCount()-1);

and reverse fragment index in adapter like this :

    @Override
    public Fragment getItem(int position) {
        return SuperAwesomeCardFragment.newInstance(TITLES.length-1-position);
    }

Upvotes: 1

Related Questions