0

我在 android studio 中创建了选项卡式视图,但是我遇到了一个问题,当我从主页滑动到类别或收藏时,我将图标放在文本标题主页、类别和收藏的上方它工作正常,但是当我调用此方法时,图标仅显示一次,当我打开应用程序并滑动到另一个类时,它们会消失

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

0 回答 0