在TabLayout支持库

时间:2015-06-24 11:14:23

标签: android android-support-library android-tabs

在我的应用程序中,我使用支持库中的TabLayout和视图pager。我有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;
        }


    }

FragA

{

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

3 个答案:

答案 0 :(得分:11)

而不是

viewPager.setCurrentItem(tab.getPosition());

你可以使用,

tab.select();

答案 1 :(得分:0)

您是否为每个标签创建了.Xml文件。

主要活动

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

为每个选项卡/类创建Xml并在Manifest中声明类

答案 2 :(得分:0)

经过反复试验(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 ...