本文介绍了从按钮选项卡中切换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的片段,并创造了3个选项卡的tabhost并放置在第一个选项卡的按钮。我的问题是我可以改变从我创建按钮的第二个选项卡?这里是我的code:
I'm using fragments and have created a tabhost with 3 tabs and placed a button in the first tab. My question is can I change to the second tab from the button I created? Here's my code:
private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
/**
*
* @author mwho
*
*/
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set
// the
// tab
// as
// per
// the
// saved
// state
}
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab1").setIndicator("Home"),
(tabInfo = new TabInfo("Tab1", HomeTab.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab2").setIndicator("Taping Prep"),
(tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab3").setIndicator("Tab 3"),
(tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener(this);
}
/**
* @param activity
* @param tabHost
* @param tabSpec
* @param clss
* @param args
*/
private static void addTab(ElastoTabsActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
tabInfo.fragment = activity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager()
.beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
谢谢!
推荐答案
你有机会阅读的?因为你已经在使用广告code键更改标签。
Did you have a chance to read the TabHost documentary before writing your code? Because you are already using a function in your code to change the tabs.
您需要做的唯一的事情就是实现它的按钮的onClick监听器里。如果你不知道如何处理按钮点击,有广阔的网络上的例子。
The only thing you have to do is to implement it inside your button's onClick listener. If you don't know how to handle button clicks, there is a vast of examples on the web.
这篇关于从按钮选项卡中切换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!