更改选项卡主机意图

时间:2011-12-25 09:20:33

标签: android android-tabhost

我正在开发一个从互联网上提取信息的应用程序。信息分为类别,子类别和子子类别。

我的主视图是带有3个选项卡的TabHost视图(父类别)和初始列表视图(子类别)。当用户单击列表视图中的项目时,它会调用一个新的列表视图,该视图显示所选子类别的子类别。

除了当选择子类别时tabHost视图消失并且子子类别全屏显示,我完成了所有工作。

如何更改选项卡的意图以显示子类别的子类别?

编辑:这是我的代码,抱歉我没有提前发布!

我的主视图包含tabhost:

public class tabwidget  extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, category1Activity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("category1").setIndicator("Category1",
                      res.getDrawable(R.drawable.ic_tab_category1))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, category2Activity.class);
    spec = tabHost.newTabSpec("category2").setIndicator("Category2",
                      res.getDrawable(R.drawable.ic_tab_category2))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, category3Activity.class);
    spec = tabHost.newTabSpec("category3").setIndicator("Category3",
                      res.getDrawable(R.drawable.ic_tab_category3))
                  .setContent(intent);
    tabHost.addTab(spec);


    tabHost.setCurrentTab(0);
}

启动应用程序时,默认情况下会选择酒精选项卡。这是category1Acitivity列表视图,其中包含调用子类别的onlclick操作:

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
          //Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
          //Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(), "Loading "+((TextView) view.findViewById(R.id.categoryname)).getText(),
            Toast.LENGTH_SHORT).show();

            Intent i = new Intent(category1Activity.this, subCategoryActivity.class);
            i.putExtra("id", ((TextView) view.findViewById(R.id.message)).getText());
            i.putExtra("catname", ((TextView) view.findViewById(R.id.categoryname)).getText());
            i.putExtra("parentcatid", "0");
            startActivityForResult(i, ACTIVITY_CREATE);




        }
    });

列表视图由类别ID生成,发送到服务器的类别ID从数据库中提取结果。

1 个答案:

答案 0 :(得分:1)

您必须使用ActivityGroups来执行此操作。

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

但是,请记住,在ICS中不推荐使用ActivityGroups。

编辑:这是我对ActivityGroup的实现:

标签中的活动:

Intent i = new Intent(v.getContext(), SearchList.class);
i.putExtra("search", search);

View view = SearchActivityGroup.group.getLocalActivityManager()  
.startActivity("SearchList", i  
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
.getDecorView();  

// Again, replace the view  
SearchActivityGroup.group.replaceView(view);

的ActivityGroup:

package nl.dante.SuperDeals;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SearchActivityGroup extends ActivityGroup {

View rootView;

// Keep this in a static variable to make it accessible for all the nested
// activities, lets them manipulate the view
public static SearchActivityGroup group;

// Need to keep track of the history if you want the back-button to work
// properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*
     * this.history = new ArrayList<View>(); group = this;
     * 
     * // Start the root activity within the group and get its view View
     * view = getLocalActivityManager().startActivity("Search", new
     * Intent(this,Search.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
     * .getDecorView();
     * 
     * // Replace the view of this ActivityGroup replaceView(view);
     */

}

@Override
protected void onResume() {

    super.onResume();
    this.history = new ArrayList<View>();
    group = this;

    // Start the root activity within the group and get its view
    View view = getLocalActivityManager().startActivity("Search", new Intent(this, Search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

    // Replace the view of this ActivityGroup
    replaceView(view);
}

public void replaceView(View v) {
    // Adds the old one to history
    if (history.size() == 0) {
        if (rootView != null) {
            history.add(rootView);
            rootView = null;
        }
    }
    history.add(v);
    // Changes this Groups View to the new View.
    setContentView(v);
}

public void back() {
    try {
        if (history.size() > 0) {
            if (history.size() == 1) {
                rootView = history.get(0);
                Toasts.ToastImageView(this, "Druk nogmaals BACK om af te sluiten", R.drawable.power_64_off, "red");
            }
            history.remove(history.size() - 1);
            setContentView(history.get(history.size() - 1));
        } else {
            finish();
        }
        if (history.size() < 3) {
            // Tabhost.bannerImage2.setImageResource(0);
            Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue);
        }
        if (history.size() == 2) {
            Tabhost.bannerImage1.setImageResource(R.drawable.sorteer_btn);
        }
    } catch (Exception ex) {
    }
}

public int getHistorySize() {
    return history.size();
}

@Override
public void onBackPressed() {
    try {
        SearchActivityGroup.group.back();
    } catch (Exception ex) {

    }
    return;
}
}