Android 更改选项卡主机意图
我正在开发一个从互联网上获取信息的应用程序。信息分为类别、子类别和、子类别 我的主视图是一个包含3个选项卡的TabHost视图(父类别)和初始列表视图(子类别)。当用户单击列表视图中的项目时,它将调用一个新的列表视图,该视图显示所选子类别的子类别 我做了所有的工作,除了选择子类别时,tabHost视图消失,子类别全屏显示 如何更改选项卡的意图以显示子类别的子类别 编辑:这是我的代码,很抱歉我没有早点发布 包含tabhost的我的主视图:Android 更改选项卡主机意图,android,android-tabhost,Android,Android Tabhost,我正在开发一个从互联网上获取信息的应用程序。信息分为类别、子类别和、子类别 我的主视图是一个包含3个选项卡的TabHost视图(父类别)和初始列表视图(子类别)。当用户单击列表视图中的项目时,它将调用一个新的列表视图,该视图显示所选子类别的子类别 我做了所有的工作,除了选择子类别时,tabHost视图消失,子类别全屏显示 如何更改选项卡的意图以显示子类别的子类别 编辑:这是我的代码,很抱歉我没有早点发布 包含tabhost的我的主视图: public class tabwidget exten
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);
}
启动应用程序时,默认情况下选择酒精选项卡。这是Category1Activity列表视图,具有调用子类别的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);
}
});
listView.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、int位置、长id){
//Toast.makeText(getApplicationContext(),“您在位置“+位置单击了项,
//吐司。长度(短)。show();
Toast.makeText(getApplicationContext(),“Loading”+((TextView)视图.findViewById(R.id.categoryname)).getText(),
吐司。长度(短)。show();
意向i=新意向(category1Activity.this,subCategoryActivity.class);
i、 putExtra(“id”,((TextView)view.findviewbyd(R.id.message)).getText();
i、 putExtra(“catname”,((TextView)view.findViewById(R.id.categoryname)).getText();
i、 putExtra(“parentcatid”、“0”);
startActivityForResult(i,活动\创建);
}
});
列表视图是由发送到服务器的类别Id生成的,它从数据库中提取结果。您必须使用它来完成此操作
但是,请记住,ICS中不推荐使用ActivityGroup
编辑:这是我对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;
}
}
选项卡中的活动:
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);
活动组:
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;
}
}
包nl.dante.SuperDeals;
导入java.util.ArrayList;
导入android.app.ActivityGroup;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
公共类SearchActivityGroup扩展ActivityGroup{
视图根视图;
//将其保存在静态变量中,以使所有嵌套的
//活动,让它们操纵视图
公共静态搜索活动组;
//如果希望“后退”按钮正常工作,则需要跟踪历史记录
//正确地说,如果你的活动需要大量内存,就不要使用它。
私家侦探史;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/*
*this.history=new ArrayList();group=this;
*
*//启动组内的根活动并获取其视图
*view=getLocalActivityManager().startActivity(“搜索”),新建
*Intent(this,Search.class).addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP))
*.getDecorView();
*
*//替换此ActivityGroup的视图replaceView(视图);
*/
}
@凌驾
受保护的void onResume(){
super.onResume();
this.history=new ArrayList();
组=此;
//在组内启动根活动并获取其视图
查看视图=getLocalActivityManager().startActivity(“搜索”,新意图(此,Search.class).addFlags(意图.FLAG\u活动\u清除\u顶部)).getDecorView();
//替换此ActivityGroup的视图
替换视图(视图);
}
公共视图(视图v){
//将旧的添加到历史记录中
if(history.size()==0){
if(rootView!=null){
添加(rootView);
rootView=null;
}
}
历史。添加(v);
//将此组视图更改为新视图。
setContentView(v);
}
公众退票(){
试一试{
if(history.size()>0){
if(history.size()==1){
rootView=history.get(0);
祝酒。祝酒视图(此“Druk nogmaals BACK om af te sluiten”,R.drawable.power_64_off,“红色”);
}
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}否则{
完成();
}
if(history.size()<3){
//Tabhost.bannerImage2.setImageResource(0);
Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue);
}
if(history.size()==2){
Tabhost.bannerImage1.setImageResource(R.drawable.sorter_btn);
}
}捕获(例外情况除外){
}
}
public int getHistorySize(){
返回history.size();
}
@凌驾
public void onBackPressed(){
试一试{
SearchActivityGroup.group.back();
}捕获(例外情况除外){
}
返回;
}
}
非常感谢您!这是完美的,经过一点微调,我成功地让它工作:)