在 TabLayout 支持库中以编程方式设置选项卡指示器位置

Set tab indicator position programatically in TabLayout support library

在我的应用程序中,我使用支持库中的 TabLayout 和视图 pager.I 在 it.Suppose 中有 3 个片段,我在 fragA 中,它有一个按钮,点击后我会转到 fragB.I我成功地转到了 fragB,但唯一的问题是标签指示器仍然在 fragA。

代码

 private void setupTablayout() {


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(1, true);

        tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
        tabLayout.getTabAt(1).setIcon(R.drawable.status_1).setText("");
        tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");

        tvHeader.setText("STATUS");

        tabLayout.setOnTabSelectedListener(this);
        //tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));


    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        viewPager.setCurrentItem(tab.getPosition());
        switch (tab.getPosition()) {
            case 0:

                tab.setIcon(R.drawable.archive_1).setText("");
                tvHeader.setText("ARCHIVES");
                tvDate.setVisibility(View.GONE);
                ivRefresh.setVisibility(View.VISIBLE);

                break;
            case 1:
                tab.setIcon(R.drawable.status_1).setText("");
                tvHeader.setText("STATUS");
                tvDate.setVisibility(View.VISIBLE);
                ivRefresh.setVisibility(View.VISIBLE);
                break;
            case 2:
                tab.setIcon(R.drawable.settings_1).setText("");
                tvHeader.setText("SETTINGS");
                tvDate.setVisibility(View.GONE);
                ivRefresh.setVisibility(View.GONE);
                break;


        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {


        viewPager.setCurrentItem(tab.getPosition());
        switch (tab.getPosition()) {
            case 0:

                tab.setIcon(R.drawable.archive).setText("");

                break;
            case 1:
                tab.setIcon(R.drawable.status).setText("");
                break;
            case 2:
                tab.setIcon(R.drawable.settings).setText("");
                break;


        }

    }


    @Override
    public void onTabReselected(TabLayout.Tab tab) {

        viewPager.setCurrentItem(tab.getPosition());
        switch (tab.getPosition()) {
            case 0:

                tab.setIcon(R.drawable.archive).setText("");

                break;
            case 1:
                tab.setIcon(R.drawable.status).setText("");

                break;
            case 2:
                tab.setIcon(R.drawable.settings).setText("");
                break;


        }

    }

    class MyPagerAdapter extends FragmentPagerAdapter {


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

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = null;

            switch (position) {
                case 0:

//                    tabLayout.getTabAt(0).setIcon(R.drawable.archive_1).setText("");
//                    tabLayout.getTabAt(1).setIcon(R.drawable.status).setText("");
//                    tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");
                    fragment = new ArchivesFrag();


                    break;
                case 1:
//                    tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
//                    tabLayout.getTabAt(1).setIcon(R.drawable.status_1).setText("");
//                    tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");
                    fragment = StatusFrag.newInstance(listPosition);

                    break;
                case 2:
//                    tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
//                    tabLayout.getTabAt(1).setIcon(R.drawable.status).setText("");
//                    tabLayout.getTabAt(2).setIcon(R.drawable.settings_1).setText("");
                    fragment = new SettingFrag();

                    break;


            }
            return fragment;
        }

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


    }

片段A

{

        ((Home) getActivity()).getArchiveList();
    }

您是否为每个选项卡创建了 .Xml 文件。

主要Activity

public class MainActivity extends TabActivity
{
TabHost tabHost; 
TabSpec tab1,tab2,tab3,tab4;
/** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // create the TabHost that will contain the Tabs
                 tabHost = (TabHost)findViewById(android.R.id.tabhost);
                // tabHost.setOnTabChangedListener(this);


                TabSpec tab1 = tabHost.newTabSpec("First Tab");
                TabSpec tab2 = tabHost.newTabSpec("Second Tab");
                TabSpec tab3 = tabHost.newTabSpec("Third tab");
                TabSpec tab4 = tabHost.newTabSpec("Fourth Tab");

               // Set the Tab name and Activity
               // that will be opened when particular Tab will be selected

                tab1.setIndicator("",getResources().getDrawable(R.drawable.video));
                tab1.setContent(new Intent(this,VideoActivity.class));
                //tab1.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));

                tab2.setIndicator("",getResources().getDrawable(R.drawable.images));
                tab2.setContent(new Intent(this,ImagesActivity.class));
                //tab2.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));

                tab3.setIndicator("",getResources().getDrawable(R.drawable.audio));
                tab3.setContent(new Intent(this,AudioActivity.class));
                //tab3.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));

                tab4.setIndicator("",getResources().getDrawable(R.drawable.favourites));
                tab4.setContent(new Intent(this,Favourites.class));
                //tab4.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));

                /** Add the tabs  to the TabHost to display. */
                tabHost.addTab(tab1);
                tabHost.addTab(tab2);
                tabHost.addTab(tab3);
                tabHost.addTab(tab4);
 }

为每个 Tab/Class 制作 Xml 并在 Manifest

中声明 类

而不是

viewPager.setCurrentItem(tab.getPosition());

可以使用,

tab.select();

经过反复试验 (SDK 23),这对我有用:

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    selectedTab = savedInstanceState.getInt("seltab");
    allTabs.getTabAt(selectedTab).select(); // TabLayout
    }

@Override
    protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("seltab",selectedTab);
    super.onSaveInstanceState(outState);
    }

注意:在加载片段时设置 selectedTab...