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

      
      
          
      
          
      
          
              
          
      
      

    4. fragment_find.xml(其他的fragment和这个类似就不贴代码了)

      
      
      
          
      
      

    5. view_tab_item.xml

      
      
      
          
      
          
      
      

效果图:

FragmentTabHost的使用和重复创建问题的优化_第1张图片






你可能感兴趣的:(安卓应用开发)