简体   繁体   中英

How to update indicator image in Tab android?

I have two tabs with two indicators and its working fine

tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
        chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
        tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
        tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
    } else {
        peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
        chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
        tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
        tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
    }
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_bg_selected);
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_bg_unselected);

Now i need to change the indicator on tab change i have tried using setting the indicator again with using the method but it wont work any one help me

 @Override
public void onTabChanged(String s) {
    if (s.equals("people")) {
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.tab_bg_selected);
        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.tab_bg_unselected);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
        } else {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
        }

    } else if (s.equals("chat")) {
        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.tab_bg_selected);
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.tab_bg_unselected);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat));
        } else {
            peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group, null));
            chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat, null));
        }


    }
    tabHost.getTabWidget().getChildAt(0).setTag(peopleTabSpec.getTag());
    tabHost.getTabWidget().getChildAt(1).setTag(chatTabSpec.getTag());

}

I tried using the above code but wont work !

 ImageView peopleIndicator = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
            peopleIndicator.setImageDrawable(getResources().getDrawable(R.drawable.group_icon, null));
            ImageView chatIndicator = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
            chatIndicator.setImageDrawable(getResources().getDrawable(R.drawable.icon_chat_green, null));

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