简体   繁体   English

可滑动标签页更改了应用程序图标以及标签页图标

[英]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. 编辑: Android 4标准时钟应用程序是选项卡中图标的一个很好的示例。 The source at the link should be very useful, especially the drawable folder. 链接中的源应该非常有用,尤其是可绘制文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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