[英]Change content of tab on button click
I have been reading a lot of comments on how it is more lightweight to use views instead of intents in setContent
when using tabs. 我一直在阅读很多有关使用选项卡时使用视图而不是
setContent
的意图的轻量级方法的评论。
Since I will be creating an app with tabs, I am trying to implement this instead of having intents inside setContent
. 由于我将创建一个带有标签的应用程序,因此我试图实现这一点,而不是在
setContent
中包含意图。 However, I am having a difficult time looking for examples or straightforward tutorials on swapping the current view with another one. 但是,我很难找到有关将当前视图与另一个视图交换的示例或简单的教程。
Say I have TAB_A, with some entry fields and a button.
假设我有TAB_A,其中包含一些输入字段和一个按钮。
When the user clicks on the button, a
TextView
will display "Hello, user!"
当用户单击按钮时,
TextView
将显示“您好,用户!”。
in the same TAB_A.
在同一TAB_A中。
Of course this is an oversimplified example, but it will surely point me in the right direction.
当然,这是一个过于简化的示例,但是肯定会为我指明正确的方向。
EDIT: I have a tab called TAB_A with a button in it. 编辑:我有一个名为TAB_A的选项卡,其中有一个按钮。 If the user clicks on that button, TAB_A will now display "Hello, user!"
如果用户单击该按钮,TAB_A现在将显示“您好,用户!”。 instead of the button.
而不是按钮。
Here is a sample of my TabSpec. 这是我的TabSpec的示例。
TabHost.TabSpec spec = tabHost.newTabSpec(getString(R.string.tabspec_tag_search))
.setIndicator(getString(R.string.tabspec_indic_search),
res.getDrawable(R.drawable.tab_ic_search))
.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
switch (doWhat){
case ACTIVITY_SPLASH_PAGE:
return showButton;
case ACTIVITY_HELLO:
return showHello;
}
return null;
}
});
tabHost.addTab(spec);
Where ShowButton
and ShowHello
are classes both extending LinearLayout
. 其中
ShowButton
和ShowHello
都是扩展LinearLayout
类。 Both are initialized in onCreate like this: ShowButton showButton = new ShowButton(this);
两者都在onCreate中这样初始化:
ShowButton showButton = new ShowButton(this);
Inside the showButton class, I have this listener: 在showButton类中,我有以下侦听器:
viewHolder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Search247.class);
intent.putExtra(EXTRA_DO_WHAT, ACTIVITY_HELLO);
context.startActivity(intent);
}
});
When the app first starts, the user sees the view showButton. 当应用程序首次启动时,用户会看到视图showButton。 When the user clicks on the button, I want the tab to change its contents to showHello.
当用户单击按钮时,我希望选项卡将其内容更改为showHello。
The question then is: Is there any way to update the contents of the tabspec without using startActivity
? 然后的问题是:是否可以在不使用
startActivity
情况下更新tabspec的内容?
Thanks as always! 一如既往的感谢!
Found the answer: 找到了答案:
ViewGroup viewGroup = (ViewGroup) mView.getParent();
viewGroup.removeView(mView);
addView(showResults);
Where mView
is the initial view in the tab. 其中
mView
是选项卡中的初始视图。 Now just trying to figure out how to make the app remember to show mView
when BACK is pressed from the new view. 现在,只想弄清楚如何使应用程序记住
mView
视图中按BACK时显示mView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.