单击活动选项卡重新加载当前活动
我有一个选项卡布局,活动显示在框架布局中。 如何通过再次单击“主页”-选项卡从选项卡“主页”重新加载当前活动?
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);
}
}
i have a Tab Layout and the Activities are displayed in a frameLayout.
How can i reload the current Activity from Tab "Home" by clicking again on "Home"-Tab ?
TabTestActivity-class
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-class
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您在寻找什么,但如果您想在用户返回到特定选项卡时更改某些值,您可以在活动的 onResume 方法中执行此操作。
I'm not sure what you're looking for but if you want to change some values when the user returns to a specific tab you can do that in the onResume method of your activity.
这是我的 FirstActivity 类。
This is my FirstActivity Class.