gpt4 book ai didi

android - 单击 Activity 选项卡重新加载当前 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:02 26 4
gpt4 key购买 nike

我有一个选项卡布局, Activity 显示在 frameLayout 中。如何通过再次单击“主页”-Tab 从“主页”选项卡重新加载当前 Activity ?

TabTestActivity 类

public class TabTestActivity extends TabActivity implements OnClickListener{

TabHost tabHost;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


//MENÜ////////////////////////////////////////////

/** TabHost will have Tabs */
tabHost = (TabHost)findViewById(android.R.id.tabhost);

/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");

/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator(new MyView(this, R.drawable.tabhome, "Home")).setContent(new Intent(this,FirstGroup.class));
secondTabSpec.setIndicator(new MyView(this, R.drawable.tabmsg, "MSGs")).setContent(new Intent(this,FirstGroup.class));
thirdTabSpec.setIndicator(new MyView(this, R.drawable.tabprofil, "Profil")).setContent(new Intent(this,FirstGroup.class));
fourthTabSpec.setIndicator(new MyView(this, R.drawable.tabmehr, "Mehr...")).setContent(new Intent(this,FirstGroup.class));

/** Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.addTab(fourthTabSpec);

tabHost.setCurrentTab(0);
}

//LAYOUT OF TABS
private class MyView extends LinearLayout {
public MyView(Context c, int drawable, String label) {
super(c);

LinearLayout la = new LinearLayout(c);
//la.setBackgroundColor(Color.parseColor("#3b5091"));
la.setOrientation(LinearLayout.VERTICAL);
la.setMinimumHeight(63);
la.setPadding(0,8,0,0);

ImageView iv = new ImageView(c);
TextView tv = new TextView(c);

iv.setImageResource(drawable);

setPadding(0,0,2,0);
setOrientation(LinearLayout.VERTICAL);

tv.setText(label);
tv.setGravity(0x01); /* Center */

tv.setTextColor(Color.WHITE);

la.addView(iv);
la.addView(tv);
addView(la);
setBackgroundDrawable( getResources().getDrawable(R.drawable.tab_indicator));

}

}

FirstGroup级

public class FirstGroup extends ActivityGroup {  

// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static FirstGroup 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 withing the group and get its view
View view = getLocalActivityManager().startActivity("CitiesActivity", new
Intent(this,CitiesActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
.getDecorView();

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

}

public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}

public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}else {
finish();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
FirstGroup.group.back();
return true;
}
return super.onKeyDown(keyCode, event);
}

}

最佳答案

我不确定您在寻找什么,但如果您想在用户返回到特定选项卡时更改某些值,您可以在 Activity 的 onResume 方法中执行此操作。

关于android - 单击 Activity 选项卡重新加载当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6557937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com