Android 片段中的制表符
我的android应用程序有两个片段 1.左一(即菜单列表) 2.右一(即左菜单中一项的详细信息) 我面临的问题是在每个详细信息屏幕(右侧)上添加选项卡 我搜索了很多标签“tabs inside fragment”或“how to AddTabHost in a fragment”等 请帮帮我。Android 片段中的制表符,android,android-fragments,android-tabhost,Android,Android Fragments,Android Tabhost,我的android应用程序有两个片段 1.左一(即菜单列表) 2.右一(即左菜单中一项的详细信息) 我面临的问题是在每个详细信息屏幕(右侧)上添加选项卡 我搜索了很多标签“tabs inside fragment”或“how to AddTabHost in a fragment”等 请帮帮我。 我被困在里面了!:-(使用最新的支持库,您可以在片段中使用ViewPager。ViewPager在类似选项卡的视图中显示片段(例如在Play Store中)。ViewPager中的“选项卡”也是片段。我
我被困在里面了!:-(使用最新的支持库,您可以在片段中使用ViewPager。ViewPager在类似选项卡的视图中显示片段(例如在Play Store中)。ViewPager中的“选项卡”也是片段。我面临同样的问题,此解决方案对我有效: MainActivity-此活动包含actionbar选项卡(不是tabhost) 片段1(表1) Fragment2(tab2)-此片段左侧有一个内部线性布局,用于显示权重为1的菜单,右侧有另一个线性布局,用于显示详细信息。在右侧线性布局中,我设置了一个放射组的样式,以显示类似于此放射组的选项卡(类似于此:但带有选项卡资产)然后我将更改linearlayouts以模拟细节片段内的选项卡 我希望你明白这个想法试试这个
public class TabBar extends FragmentActivity implements
TabHost.OnTabChangeListener {
public static Context mContext;
private TabHost mTabHost;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabBar.TabInfo>();
private TabInfo mLastTab = null;
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;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
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"));
}
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// mTabHost.getTabWidget().setStripEnabled(false);
TabInfo tabInfo = null;
TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab1"), (tabInfo = new TabInfo("Tab1", MyMapFragment.class, args)), "Favourite",
R.drawable.frd_tab_select_custom);
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab2"), (tabInfo = new TabInfo(
"Tab2", PrflTab.class, args)), "Favourite",
R.drawable.me_tab_select_custom);
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabBar.addTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab3"), (tabInfo = new TabInfo("Tab3", RequestFragment.class, args)), "Favourite",
R.drawable.request_tab_select_custom);
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 View prepareTabView(Context context, String text,
int drawable) {
View view = LayoutInflater.from(context).inflate(
R.layout.tab_indicator, null);
((ImageView) view.findViewById(R.id.icon)).setImageResource(drawable);
return view;
}
private static void addTab(TabBar activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo, String title, int drawable) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
View view = prepareTabView(tabHost.getContext(), title, drawable);
tabSpec.setIndicator(view);
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);
boolean flag = activity.getIntent().getBooleanExtra("notifi_falg", false);
if (flag) {
tabHost.setCurrentTab(2);
}
}
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.tab_1, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
@Override
public void onStop() {
super.onStop();
}
}
公共类选项卡栏扩展了FragmentActivity实现
TabHost.OnTabChangeListener{
公共静态语境;
私有TabHost-mTabHost;
私有HashMap mapTabInfo=新HashMap();
private TabInfo mLastTab=null;
私有类TabInfo{
私有字符串标签;
私家级CLS;
私有包args;
私有片段;
TabInfo(字符串标记、类clazz、包参数){
this.tag=tag;
this.clss=clazz;
this.args=args;
}
}
类TabFactory实现TabContentFactory{
私有最终上下文mContext;
公共选项卡工厂(上下文){
mContext=上下文;
}
/**
*(非Javadoc)
*
*@see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
公共视图createTabContent(字符串标记){
视图v=新视图(mContext);
v、 设置最小宽度(0);
v、 设置最小高度(0);
返回v;
}
}
/**
*(非Javadoc)
*
*@see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs\u布局);
initialiseTabHost(savedInstanceState);
如果(savedInstanceState!=null){
mTabHost.setCurrentTabByTag(savedInstanceState.getString(“tab”));
}
}
SaveInstanceState上受保护的无效(束超出状态){
outState.putString(“tab”,mTabHost.getCurrentTabTag());//保存选项卡
//精选
super.onSaveInstanceState(超出状态);
}
私有void initialiseTabHost(Bundle args){
mTabHost=(TabHost)findViewById(android.R.id.TabHost);
mTabHost.setup();
//mTabHost.getTabWidget().setStripEnabled(false);
TabInfo TabInfo=null;
TabBar.addTab(this,this.mTabHost,this.mTabHost.newTabSpec(“Tab1”),(tabInfo=newtabinfo(“Tab1”,MyMapFragment.class,args)),“收藏夹”,
R.可绘制。frd\u选项卡\u选择\u自定义);
this.mapTabInfo.put(tabInfo.tag,tabInfo);
TabBar.addTab(this,this.mTabHost,this.mTabHost.newTabSpec(“Tab2”),(tabInfo=newtabinfo(
“Tab2”,PrflTab.class,args)),“收藏夹”,
R.可绘制。me\u选项卡\u选择\u自定义);
this.mapTabInfo.put(tabInfo.tag,tabInfo);
TabBar.addTab(this,this.mTabHost,
this.mTabHost.newTabSpec(“Tab3”),(tabInfo=newtabinfo(“Tab3”,RequestFragment.class,args)),“favorite”,
R.可绘制。请求(选项卡(选择)(自定义);
this.mapTabInfo.put(tabInfo.tag,tabInfo);
//默认设置为第一个选项卡
本表已更改(“表1”);
mTabHost.setOnTabChangedListener(此);
}
/**
*@param活动
*@param tabHost
*@param tabSpec
*@param-clss
*@param args
*/
私有静态视图prepareTabView(上下文、字符串文本、,
整数(可绘制){
视图=布局更平坦。从(上下文)。充气(
R.layout.tab_指示器,空);
((ImageView)view.findViewById(R.id.icon)).setImageResource(可绘制);
返回视图;
}
私有静态void addTab(TabBar活动、TabHost TabHost、,
TabHost.TabSpec TabSpec,TabInfo TabInfo,字符串标题,int可绘制){
//将选项卡视图工厂附着到等级库
tabSpec.setContent(activity.new TabFactory(activity));
String tag=tabSpec.getTag();
View=prepareTabView(tabHost.getContext(),标题,可绘制);
tabSpec.setIndicator(视图);
tabInfo.fragment=activity.getSupportFragmentManager().findFragmentByTag(标记);
if(tabInfo.fragment!=null&&!tabInfo.fragment.isDetached()){
FragmentTransaction ft=activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
布尔标志=activity.getIntent().getBooleanExtra(“notifi_falg”,false);
国际单项体育联合会(旗){
tabHost.setCurrentTab(2);