请教各位大神一些关于fragment实现tabhostTabHost 的问题

在这里放一个使用FragmentTabHost实现的Tab页实例,由于自己对Android开发并不十分熟悉,因此无法作出一些详细的讲解,仅仅是个例子。
先放两张例子运行时截取的图片:
MainActivity的布局文件(activity_main.xml)
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical&&
&FrameLayout
android:id=&@+id/fragment&
android:layout_width=&match_parent&
android:layout_height=&0dp&
android:layout_weight=&1&/&
&android.support.v4.app.FragmentTabHost
android:id=&@+id/tab_host&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&/&
&/LinearLayout&
这个布局文件在AndroidStudio中会报异常,不过例子写完整之后运行正常,暂时没有查到原因,AndroidStudio报的错误如下所示:
MainActivity类
package com.tysoft.
import android.os.B
import android.support.v4.app.FragmentA
import android.support.v4.app.FragmentTabH
import android.view.V
import android.widget.ImageV
import android.widget.TabH
import android.widget.TextV
import android.widget.T
import com.tysoft.tab.fragment.FirstF
import com.tysoft.tab.fragment.FourthF
import com.tysoft.tab.fragment.SecondF
import com.tysoft.tab.fragment.ThirdF
import java.util.A
public class MainActivity extends FragmentActivity {
private String[] tabIds = {&DASHBOARD&, &MESSAGE&, &DATA&, &SETTING&};
private String[] tabLabels = {&工作台&, &消息&, &数据&, &设置&};
private int[] tabImgs = new int[]{R.drawable.main_tab_dashboard_img,
R.drawable.main_tab_message_img, R.drawable.main_tab_data_img,
R.drawable.main_tab_setting_img};
private Class[] tabFragmentCls = new Class[]{FirstFragment.class, SecondFragment.class,
ThirdFragment.class, FourthFragment.class};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fragment);
fragmentTabHost.getTabWidget().setDividerDrawable(null);
for (int i = 0; i & tabIds. i++) {
fragmentTabHost.addTab(fragmentTabHost.newTabSpec(tabIds[i]).setIndicator
(getTabItemView(i)), tabFragmentCls[i], null);
fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_LONG).show();
* 给Tab按钮设置图标和文字
private View getTabItemView(int index) {
View view = getLayoutInflater().inflate(R.layout.main_tab_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.main_tab_img);
imageView.setImageResource(tabImgs[index]);
TextView textView = (TextView) view.findViewById(R.id.main_tab_label);
textView.setText(tabLabels[index]);
可以对FragmentTabHost 的TabWidget作一些设置,比如最小高度或者背景等,TabWidget可以通过getTabWidget方法获取,如果下所示:
fragmentTabHost.getTabWidget().setDividerDrawable(null);
fragmentTabHost.getTabWidget().setBackgroundResource(R.drawable.ic_new_tab_p);
fragmentTabHost.getTabWidget().setMinimumHeight(300);
用到的Fragment(这里只放置了其中一个Fragment的代码,其它几个类似,仅仅内容不同),这里为了简单没有使用XML布局文件,Fragment中的内容都是在代码中生成,仅仅有一个TextView控件。
package com.tysoft.tab.
import android.os.B
import android.support.v4.app.F
import android.view.G
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.LinearL
import android.widget.TextV
public class FirstFragment extends Fragment {
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setGravity(Gravity.CENTER);
TextView textView = new TextView(getActivity());
textView.setTextSize(30);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText(&第一个Fragment&);
linearLayout.addView(textView);
return linearL
Tab页签项布局XML文件
不知为什么FragmentTabHost的Tab页不能放置图片,不符合自己的要求;幸好TabHost.TabSpec类的setIndicator支持自定义的View,因此需要一个自定义的XML布局文件,如下所示
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:gravity=&center&
android:orientation=&vertical&&
&ImageView
android:id=&@+id/main_tab_img&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&/&
android:id=&@+id/main_tab_label&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&/&
&/LinearLayout&
Tab页签中ImageView对应XML图片资源文件(仅仅只放一个,其它类似,只是图片不同而已),点击时显示与正常情况下不同的图片
&?xml version=&1.0& encoding=&utf-8&?&
&selector xmlns:android=&/apk/res/android&&
&item android:drawable=&@mipmap/icon_dashboard_pressed& android:state_selected=&true&/&
&item android:drawable=&@mipmap/icon_dashboard&/&
&/selector&
无相关信息[android]类型: Fragment
已经添加在 tabhost fragment
09-19 12:23:01.084: E/AndroidRuntime(24169): FATAL EXCEPTION: main
09-19 12:23:01.084: E/AndroidRuntime(24169): Process: com.example.loan, PID: 24169
09-19 12:23:01.084: E/AndroidRuntime(24169): java.lang.IllegalStateException: Fragment already added: FormFragment{428f10c8 #1 id=0x7f050055 form}
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1192)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:722)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:489)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.os.Handler.handleCallback(Handler.java:733)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.os.Handler.dispatchMessage(Handler.java:95)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.os.Looper.loop(Looper.java:136)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at android.app.ActivityThread.main(ActivityThread.java:5068)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at java.lang.reflect.Method.invokeNative(Native Method)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at java.lang.reflect.Method.invoke(Method.java:515)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
09-19 12:23:01.084: E/AndroidRuntime(24169):
at dalvik.system.NativeStart.main(Native Method)
有, android 应用程序, build 与 tabhost 。有三个选项卡总数,tab2,还有 button ,使 fragment tab2 的交易 (这调用函数 fragment activity )
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.realtabcontent, mFrag);
t.addToBackStack(null);
t.commit();
如果我这样跑,还有异常:
里面的 tab2,我按下 button 以更改fragment
转到其他选项卡 (eg。 1 标签或标签 3)
回到新闻button
如何解决这个问题?谢谢你帮助
解决方法 1:
你只需要检查一个条件你 fragment 下面提到:
if(!isAdded())
isAdded = 返回 true,如果 fragment 当前添加到其 activity .来自官方的文档。这不会增加, fragment 如果它已添加
请检查下面的链接以供参考:请教各位大神一些关于FragmentTabHost 的问题_百度知道
请教各位大神一些关于FragmentTabHost 的问题
我有更好的答案
0七六五二零五六零零 希望结交更多喜欢音乐的朋友,帮他们免费作曲编曲。这样您好,我可以帮你免费编哦0,但是作词。虽然算不上大神,现在我正准备给2014名贫穷的孩子完成他们的音乐梦想、编一般的曲还是略懂一二的、作曲,你把你的词曲谱和相关要求给我
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁IT怪 微社区开放
IT怪微社区开放 在IT怪记录自己的疑难笔记,好记心不如烂笔头,成长一点一滴的记录
, 如果您想提高自己的技术水平,认识同行朋友、开拓技术视野请加入、技术大牛请加入、HR请加入请教大家个问题,fragmenttabhost fragment大家怎么避免fragment...
- eoeAndroid
请教大家个问题,fragmenttabhost fragment大家怎么避免fragment...
请教大家个问题,fragmenttabhost fragment大家怎么避免fragment重新绘制UI ?有啥好的方法?
重新绘制不是很好么?帮你节省内存开销!
其他回答 (2)
使用下面程序代码修改:[mw_shl_code=java,true]& & & &&&*/& & & & private void initView(){& & & & & & & & //实例化布局对象& & & & & & & & layoutInflater = LayoutInflater.from(this);& & & & & & & & & & & & & & & &&& & & & & & & & //实例化TabHost对象,得到TabHost& & & & & & & & mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);& & & & & & & & mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);& & & &&& & & & & & & &&& & & & & & & & //得到fragment的个数& & & & & & & & int count = fragmentArray.& & & &&& & & & & & & & & & & & & & & &&& & & & & & & & for(int i = 0; i & i++){& & & &&& & & & & & & & & & & & //为每一个Tab按钮设置图标、文字和内容& & & & & & & & & & & & TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray).setIndicator(getTabItemView(i));& & & & & & & & & & & & //将Tab按钮添加进Tab选项卡中& & & & & & & & & & & & mTabHost.addTab(tabSpec, fragmentArray, null);& & & & & & & & & & & & //设置Tab按钮的背景& & & & & & & & & & & & mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);& & & & & & & & }& & & & }
你可以对他进行缓存,判断是否等null 如果是null就加载并且保存 下次进来时就不会重新加载了 吧
相关知识等待您来回答
该问题来自:eoeAndroid 开发门户,中国最大最活跃的Android开发社区。电脑常识领域专家
& &SOGOU - 京ICP证050897号

我要回帖

更多关于 tabhost 的文章

 

随机推荐