Set tab indicator position programatically in TabLayout support library

怎甘沉沦 提交于 2020-01-03 16:49:41

问题


In my application i am using TabLayout from support library with view pager.I have 3 fragments in it.Suppose i am in fragA which has a button which on clicked takes me to fragB.I am sucessfully going to fragB but only problem is that the tab indicator remains at fragA.

Code

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


    }

FragA

{

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

回答1:


Instead of

viewPager.setCurrentItem(tab.getPosition());

You can use,

tab.select();



回答2:


Have you Created the .Xml file for each Tab.

Main 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);
 }

Make Xml For Each Tab/Class And Declare the classes in Manifest




回答3:


This worked for me, after trial and error (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);
    }

Note: Set selectedTab when the fragment is loaded...



来源:https://stackoverflow.com/questions/31025044/set-tab-indicator-position-programatically-in-tablayout-support-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!