FragmentTabHost的使用和重复创建问题的优化
来源:互联网 发布:windows10不兼容软件 编辑:程序博客网 时间:2024/06/05 01:08
首先使用FragmentTabhost的Activity必须继承自FragmentActivity
public class MainActivity extends FragmentActivity
调用setup方法进行FragmentManager和用来显示fragment的FrameLayout的绑定。
mTabHost.setup(this, getSupportFragmentManager(), R.id.main_frame);
调用newTabSpec创建tab选项
TabSpec tabSpec = mTabHost.newTabSpec(getString(mTitleID[i])).setIndicator(getItemView(i));
调用addTab进行tab选项和fragment类进行绑定。
mTabHost.addTab(tabSpec,mFragmentsClass[i], null);
调用mTabHost.getTabWidget().setDividerDrawable(null)去除每个tab之间的分割线。
mTabHost.getTabWidget().setDividerDrawable(null);
FragmentTabhost的优化:(防止FragmentTabhost切换fragment时不断销毁和创建view)。
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubif (mCacheView == null) {Log.i("Test", "FindFragment onCreateView");mCacheView = inflater.inflate(R.layout.fragment_find, null);}ViewGroup parent = (ViewGroup) mCacheView.getParent();if (parent != null) {parent.removeView(mCacheView);}return mCacheView;}
详细代码。
MainActivity.java
package com.example.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost.TabSpec;import android.widget.TextView;public class MainActivity extends FragmentActivity {private FragmentTabHost mTabHost;private Class mFragmentsClass[] = { NewsFragment.class,RecreationFragment.class, MediaFragment.class, FindFragment.class };private int mTitleID[] = { R.string.news, R.string.recreation,R.string.meidia, R.string.find };private int mImageID[] = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher };private final int mItemCount = 4;private LayoutInflater mInflater;@Overrideprotected void onCreate(Bundle arg0) {// TODO Auto-generated method stubsuper.onCreate(arg0);setContentView(R.layout.activity_main);initView();}private void initView() {mInflater = LayoutInflater.from(this);mTabHost = (FragmentTabHost) findViewById(R.id.main_tabhost);mTabHost.setup(this, getSupportFragmentManager(), R.id.main_frame);for (int i = 0; i < mItemCount; i++) {TabSpec tabSpec = mTabHost.newTabSpec(getString(mTitleID[i])).setIndicator(getItemView(i));mTabHost.addTab(tabSpec,mFragmentsClass[i], null);}mTabHost.getTabWidget().setDividerDrawable(null); }//单个tab选项卡的视图获取。private View getItemView(int index) {View v = mInflater.inflate(R.layout.view_tab_item, null);ImageView imageView = (ImageView) v.findViewById(R.id.tab_item_image);imageView.setBackgroundResource(mImageID[index]);TextView textView = (TextView) v.findViewById(R.id.tab_item_text);textView.setText(getString(mTitleID[index]));return v;}}
FindFragment.java(其他的fragment和这个类似就不贴代码了)
package com.example.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FindFragment extends Fragment{private View mCacheView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubif (mCacheView == null) {Log.i("Test", "FindFragment onCreateView");mCacheView = inflater.inflate(R.layout.fragment_find, null);}ViewGroup parent = (ViewGroup) mCacheView.getParent();if (parent != null) {parent.removeView(mCacheView);}return mCacheView;}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/main_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="match_parent" android:layout_height="1px" android:background="#c7c7c7" /> <android.support.v4.app.FragmentTabHost android:id="@+id/main_tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="#f4f4f4" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>
fragment_find.xml(其他的fragment和这个类似就不贴代码了)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/find_tips" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/find" /></LinearLayout>
view_tab_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/tab_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
效果图:
0 0
- FragmentTabHost的使用和重复创建问题的优化
- 【FragmentTabHost】FragmentTabHost的使用
- ViewPager和FragmentTabHost的使用
- FragmentTabHost+Fragment+Viewpage遇到的Fragment重复加载问题
- Fragment和FragmentTabHost的混合使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- fragmentTabHost的使用
- FragmentTabHost的使用
- android FragmentTabHost的使用
- FragmentTabHost的使用
- FragmentTabHost的简单使用
- FragmentTabhost的使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- FragmentTabHost的简单使用
- 技巧积累--Smack、Spark、Openfire
- Spark核心类介绍
- mybatis3.x Cannot load JDBC driver class '${jdbc.driverClassName}
- Git-0.99 README 第一部分 基本对象
- Android之android studio如何获取证书指纹 (SHA1)
- FragmentTabHost的使用和重复创建问题的优化
- iOS 异步图片加载优化与常用开源库分析
- 笔记十九(基本动画——速度实例)
- 关于Random(47)和nextint(3)的问题
- 第十二周项目3-图遍历算法实现(1)
- android(39)(创建图形的拷贝、旋转、镜子效果、颜色变化)
- 12月7日,BOOST库安装及配置
- Android Notification
- 颜色转换 IOS中十六进制的颜色转换为UIColor