开发者

Call same activity in every tab setContent()

开发者 https://www.devze.com 2023-03-13 03:49 出处:网络
I have a tab activity that every of its tabs is calling the same activity lets say MyTabInterface. So I want my MyTabInterface can take the information of which tab is pressed.I have tried to do this

I have a tab activity that every of its tabs is calling the same activity lets say MyTabInterface. So I want my MyTabInterface can take the information of which tab is pressed.I have tried to do this with th开发者_JAVA技巧e code below but didnt seem to work

intent = new Intent().setClass(this,MyTabInterface.class);
intent.putExtra("interface",tabs.elementAt(0).getAction());
intent.putExtra("data",tabs.elementAt(0).getData());
spec = tabHost.newTabSpec(tabs.elementAt(0).getName()).setIndicator(tabs.elementAt(0).getLabel().toString(),res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(spec);


intent = new Intent().setClass(this,MyTabInterface.class);
intent.putExtra("interface","table");
intent.putExtra("data","table_data");
spec= tabHost.newTabSpec(tabs.elementAt(1).getName()).setIndicator(tabs.elementAt(1).getLabel().toString(),res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);

My View will change according to the putExtra details. Here call same activity class in both tab. Fist tab show correctly. But second one show the same as first one.

How can I solve this problem ?


Here is a code snippet for TabActivity hope this helps:

public class InfralineTabWidget extends android.app.TabActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    @SuppressWarnings("unused")
    Resources res = getResources();
    TabHost tabHost = (TabHost)getTabHost();

    TabHost.TabSpec spec;
    @SuppressWarnings("unused")
    Intent intent;
 tabHost.getTabWidget().setBackgroundDrawable(getResources().getDrawable(R.drawable.bluenavbar));
    TextView  txtTab = new TextView(this);
        txtTab.setText(getString(R.string.top_news));
        txtTab.setPadding(8, 9, 8, 9);
        txtTab.setTextColor(Color.WHITE);
        txtTab.setTextSize(14);
    txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        txtTab.setBackgroundResource(R.drawable.tab_news);
    intent = new Intent().setClass(this, TopNewsGroup.class);
    spec = tabHost.newTabSpec("topNews").setIndicator(txtTab).setContent(new Intent(this,TopNewsGroup.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ));
    tabHost.addTab(spec);

    TextView  txtTab1 = new TextView(this);
    txtTab1.setText(getString(R.string.power));
    txtTab1.setPadding(8, 9, 8, 9);
    txtTab1.setTextColor(Color.WHITE);
    txtTab1.setTextSize(14);
    txtTab1.setTypeface(localTypeface1);
    txtTab1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    txtTab1.setBackgroundResource(R.drawable.tab_power);
    intent = new Intent().setClass(this, PowerGroup.class);
    spec = tabHost.newTabSpec("power").setIndicator(txtTab1).setContent(new Intent(this,PowerGroup.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ));
    tabHost.addTab(spec);


    tabHost.setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 100;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 100;

        tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 160;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = 160;    

}

}


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maiin);




        final TabHost tabHost = (TabHost) getTabHost();
        try {               

            tabHost.addTab(createTab(FIRST_ACTIVITY_NAME.class, "eter",
                    "eter", R.drawable.tab_icon_events));
            tabHost.addTab(createTab(SECONDACTIVITY_NAME.class, ".abc", ".abc",
                    R.drawable.tab_icon_pitchforkfm));
            tabHost.addTab(createTab(THIRD_ACTIVTY_NAME.class, "xyz", "xyz",
                    R.drawable.tab_icon_home));
            tabHost.addTab(createTab(ExtraInfromation.class, "wewr", "wewr",
                    R.drawable.tab_icon_tv));       


            tabHost.setCurrentTab(1);
        } catch (Exception e) {
            // TODO: handle exception
        }    
        tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 85;

        tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = 85;
        tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = 85;
        tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = 85;
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabID) {
        // TODO Auto-generated method stub
        if( tabID.equals(".xyz") ){


        //DO SOMETHING 
        }

    }
});
    }



    private TabSpec createTab(final Class<?> intentClass, final String tag,
            final String title, final int drawable) {
        final Intent intent = new Intent().setClass(this, intentClass);

        final View tab = LayoutInflater.from(getTabHost().getContext())
                .inflate(R.layout.tab, null);
        ((TextView) tab.findViewById(R.id.tab_text)).setText(title);
        ((ImageView) tab.findViewById(R.id.tab_icon))
                .setImageResource(drawable);

        return getTabHost().newTabSpec(tag).setIndicator(tab)
                .setContent(intent);

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消