how can i solve issue with icons in tabbed view

42 views Asked by At

i created tabbed view in android studio but i got an issue i put icons above the text title home,category and favorite when i slide from home to category or favorite the icons goes disappear the problem come with me in this section when i comment this section it work properly but when i call this method then the icons shows only one time when i open app and slide to another class they goes disappear

    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) {

        }
    });

and this is the method where i called icons

setupTabIcons();

check out whole section and help me out please how i can i do it

 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

There are 0 answers