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


  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)

    @Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		
    		if (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;
      
      	@Override
      	protected void onCreate(Bundle arg0) {
      		// TODO Auto-generated method stub
      		super.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;
      
      	@Override
      	public View onCreateView(LayoutInflater inflater, ViewGroup container,
      			Bundle savedInstanceState) {
      		// TODO Auto-generated method stub
      		
      		if (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>

效果图:


    1. 

    
    
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值