Android Material Design右对齐选项卡

时间:2016-08-11 09:20:41

标签: android material-design android-tabs material material-ui

我想创建一个带有标签的应用程序,如标签位于右侧的休闲图片。我尝试了很多方法,但遗憾的是他们都没有工作。请一些身体帮助。

enter image description here

更新

我的活动:

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

我可以添加/删除标签。它工作得很完美,但我不知道如何将标签rtl和标签1放在右侧。

1 个答案:

答案 0 :(得分:2)

有很多技巧可以解决这个问题:

如果 希望通过颠倒标题顺序并在第一次加载片段/活动时调用viewPager.setCurrentItem(adapter.size()-1)来支持RTL语言,那么您的问题就会得到解决。

您的FragmentAdapter应该是这样的:

 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
            }
        }
    }

但是,如果你在运行时改变应用程序的位置以前的方式是痛苦的脖子。因此,更好的选择是使用以下代码更改应用的布局方向:

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

更改区域设置并重新创建活动:

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

修改:正如您在评论中提到的那样,您需要dynamic FragmentPager,因此您可以对FragmentPager应用相同的逻辑(稍加修改)。