简体   繁体   中英

Swipable Tabs changing app icon as well as tab icon

i am currently implementing some swipable tabs into my app, i have got it all working good, the part i am struggling on is changing the icon of the tab when the user swipes.

Basically the tabs update fine when the user swipes and selects the tabs, it updates from selected to non selected, the problem is that it is also changing the actionbar main icon and i was wondering how to stop this and only update the tabs.

Here is the relevant code for the main activity let me know if you need anything else

    public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener{

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles

    private static final int[] ICONS = new int[] {
            R.drawable.ic_tab_n,
            R.drawable.ic_tab_f,
            R.drawable.ic_tab_h,
            R.drawable.ic_tab_n,
            R.drawable.ic_tab_p,
    };
    private static final int[] ICONS_SELECTED = new int[] {
            R.drawable.ic_tab_n_selected,
            R.drawable.ic_tab_f_selected,
            R.drawable.ic_tab_h_selected,
            R.drawable.ic_tab_n_selected,
            R.drawable.ic_tab_p_selected,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
//        for (String tab_name : tabs) {
//            actionBar.addTab(actionBar.newTab().setText(tab_name).setIcon(resources.getDrawable(ICONS[]))
//                    .setTabListener(this));
//        }
        for (int i=0; i < tabs.length; i++) {
            actionBar.addTab(actionBar.newTab()
                    .setIcon(ICONS[i])
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
                actionBar.setIcon(ICONS_SELECTED[position]);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
                    actionBar.setIcon(ICONS[viewPager.getCurrentItem()]);
                }
            }
        });

        viewPager.setCurrentItem(2, false);
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
        tab.setIcon(ICONS_SELECTED[tab.getPosition()]);
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        tab.setIcon(ICONS[tab.getPosition()]);
    }

}

I see this twice in your code, and it seems likely to do exactly what you don't want to happen:

  actionBar.setIcon(ICONS_SELECTED[position]);

There is no reason to change the action bar icon, so those lines should be commented out.

Edit: Setting and then resetting the tab icons probably is not going to work either. I recommend changing the code to use a state list drawable for the tab icons, where each of the five tabs gets its own state list drawable as an icon, that will automatically display the corresponding selected or unselected version of its icon, depending on the state of the tab.

Edit: The Android 4 standard clock app is a good example of icons in tabs. The source at the link should be very useful, especially the drawable folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM