1

I want to make an app with tab like fallow picture that tabs placed at right side. I try so many ways unfortunately none of them worked. Pleas some body help.

enter image description here

Update

My activity :

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);

    }
    //
    static int count = 1;
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    public void addTab(View v){
        Bundle bundle = new Bundle();
        bundle.putInt("index", count);
        MyFragment fragment = new MyFragment();
        fragment.setArguments(bundle);

        adapter.addFragment(fragment, "Tab "+count++);

        TabLayout.Tab tab = tabLayout.getTabAt(adapter.getCount()-1);
        tab.select();
    }
    //
    public void delTab(View v){
        int index = tabLayout.getSelectedTabPosition();
        adapter.removeFragment(index);
    }
    //
    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        //
        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
            notifyDataSetChanged();
        }
        //
        public void removeFragment(int position){
            mFragmentTitleList.remove(position);
            mFragmentList.remove(position);
            notifyDataSetChanged();
        }
        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

enter image description here

I can add/remove tabs. It working perfect but I do't know how to make tabs rtl and Tab 1 placed at right side.

Edric
  • 24,639
  • 13
  • 81
  • 91
Majid
  • 519
  • 1
  • 9
  • 21

1 Answers1

2

There are many tricks to handle this issue:

If only want to support RTL languages by reversing order of title and calling viewPager.setCurrentItem(adapter.size()-1) in first load of fragment/Activity your problem will be fixed.

Your FragmentAdapter should be something like this:

 public class PagerAdapter extends FragmentPagerAdapter {

        private static final int POSITION_HOME = 4; //first item from right
        private static final int POSITION_CATEGORIES = 3;

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case POSITION_HOME:
                    return HomeFragment.newInstance();
                case POSITION_CATEGORIES:
                    return CategoryFragment.newInstance();
               //other fragment
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case POSITION_HOME:

                    return "HOME";
                case POSITION_CATEGORIES:
                    return "CATEGORY";
                //other title
            }
        }
    }

But if you change Locality of application at runtime previous way is pain in neck. So better option is two change Layout direction of your app with following code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindows().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

OR

change locale and recreate activity:

 Locale.setDefault(new Locale("fa"));

Edit: As you mention in comments you need dynamic FragmentPager so you can apply same logic to you FragmentPager (with little modification).

Amir
  • 16,067
  • 10
  • 80
  • 119
  • Thanks for your answer. I want to place first item at right and next Item take place at left side of first and so on. In additional, tabs adding at run time. – Majid Aug 14 '16 at 10:08