FragmentTabHost的使用和重复创建问题的优化

来源:互联网 发布:windows10不兼容软件 编辑:程序博客网 时间:2024/06/05 01:08


  1.  首先使用FragmentTabhostActivity必须继承自FragmentActivity  

    public class MainActivity extends FragmentActivity

  2. 调用setup方法进行FragmentManager和用来显示fragmentFrameLayout的绑定。

    mTabHost.setup(this, getSupportFragmentManager(), R.id.main_frame);

  3. 调用newTabSpec创建tab选项

    TabSpec tabSpec = mTabHost.newTabSpec(getString(mTitleID[i])).setIndicator(getItemView(i));

  4. 调用addTab进行tab选项和fragment类进行绑定。

    mTabHost.addTab(tabSpec,mFragmentsClass[i], null);

  5. 调用mTabHost.getTabWidget().setDividerDrawable(null)去除每个tab之间的分割线。

    mTabHost.getTabWidget().setDividerDrawable(null); 

  6. 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;}


  7. 详细代码。

    1. 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;}}

    2. 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;}}

    3. 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>

    4. 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>

    5. 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
    原创粉丝点击