使用ViewPager后,TabItem图标消失

时间:2017-10-12 14:04:11

标签: java android android-tablayout

我的TabLayout很完美,带有图标,然后我不得不添加一个ViewPagerOnTabSelectedListener,我的图标丢失了。
有谁可以帮助我吗?

我尝试过在我的MainActivity中添加这个解决方案,但没有改变:

| Website 1           | Website 2           | Website 3 | Product 1         | Product 2          | Product 3 | Available     | Country |
|---------------------|---------------------|-----------|-------------------|--------------------|-----------|---------------|---------|
| https://google.org/ | https://google.com/ |           | http://amazon.com | https://amazon.de/ |           | Available     | Country |
| https://google.org/ | https://google.com/ |           | http://amazon.com |                    |           | Not Available | State   |

这是我的代码:

tabLayout.getTabAt(0).setIcon(R.drawable.home);

Layout.xml

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private BottomNavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);

        TabLayout tabLayout = new TabLayout(this);
        tabLayout = (TabLayout) findViewById(R.id.toolbar);

        tabLayout.setupWithViewPager(pager);

        tabLayout.addOnTabSelectedListener(
                new TabLayout.ViewPagerOnTabSelectedListener(pager) {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        super.onTabSelected(tab);
                        pager.setCurrentItem(tab.getPosition());
                        Log.i("TAG", "onTabSelected: " + tab.getPosition());

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

                    }

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

                    }
                });

        final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mAdapter);

        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_adresse:
                        pager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_cognitif:
                        pager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_deplacement:
                        pager.setCurrentItem(2);
                        return true;
                    case R.id.navigation_yeux:
                        pager.setCurrentItem(3);
                        return true;
                }
                return false;
            }

        });
    }

    class MainPagerAdapter extends FragmentPagerAdapter {

        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new MeFragment();
                case 1:
                    return new MeFragment();
                case 2:
                    return new MeFragment();
                case 3:
                default:
                    return new MeFragment();
            }
        }
        @Override
        public int getCount() {
            return 4;
        }
    } 

1 个答案:

答案 0 :(得分:1)

      tabLayout.setupWithViewPager(pager);

            tabLayout.addOnTabSelectedListener(
                    new TabLayout.ViewPagerOnTabSelectedListener(pager) {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            super.onTabSelected(tab);
                            pager.setCurrentItem(tab.getPosition());
                            Log.i("TAG", "onTabSelected: " + tab.getPosition());

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

                        }

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

                        }
                    });

            final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
            pager.setAdapter(mAdapter);
  tabLayout.setupWithViewPager(pager);

进行上述更改,问题是您在寻呼机设置适配器之前设置了viewpager,请告诉我这是否无效。