Automatically set tab indicator position in TabLayout support library

In my application, I use TabLayout from the support library with the pager.I view. There are 3 fragments in it. Suppose that I am in fragA, which has a button that is pressed, it accepts me in fragB.I successfully navigate to fragB, but the only problem is that the tab indicator remains on fragA.

the 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();
    }
+4
source share
2 answers

Instead

viewPager.setCurrentItem(tab.getPosition());

you can use

tab.select();
+11
source

Has created an .Xml file for each tab.

Primary 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 classes in the manifest

0
source

All Articles