如何解决选项卡视图中的图标问题

时间:2017-09-12 13:34:15

标签: android android-viewpager android-tabbed-activity tabbed-view

我在Android工作室创建了标签视图,但是我遇到了一个问题我把图标放在文本标题上面,类别和最喜欢当我从家里滑到类别或最喜欢的图标消失了问题来自我在这一部分当我评论此部分它正常工作,但当我调用此方法时,图标只显示一次,当我打开应用程序并滑动到另一个类,他们消失了

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.getAdapter().notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

这是我调用图标的方法

setupTabIcons();

查看整个部分并帮助我,我怎么能这样做

 viewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(viewPager);



    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.getAdapter().notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tabLayout.getSelectedTabPosition();


            if (tab.getText().equals("Categories")) {
                currentFragment = "Categories";
                LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(MainActivity.this);
                Intent i = new Intent("TAG_REFRESH");
                lbm.sendBroadcast(i);
            } else {
                currentFragment = "check_internet_connection";
            }
        }


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

        }

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

        }
    });
    setupTabIcons();

}

private void setupTabIcons() {

    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new HomeFragment(), "Home");
    adapter.addFragment(new Category(), "Categories");
    adapter.addFragment(new Favorite(), "Favorite");
    viewPager.setAdapter(adapter);
}

0 个答案:

没有答案