在我的应用程序中,我正在使用带有 View 寻呼机的支持库中的 TabLayout。我有 3 个 fragment 。假设我在 fragA 中,它有一个按钮,点击后将我带到 fragB。我成功地转到了 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;
        }


    }

fragment A
{

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

最佳答案

代替

viewPager.setCurrentItem(tab.getPosition());

您可以使用,
tab.select();

关于android - 在 TabLayout 支持库中以编程方式设置选项卡指示器位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31025044/

10-09 13:44