Android Fragment+FragmentTabHostコンポーネント実装の一般的なホームページ(微信新浪を模倣)

3564 ワード

Fragment+FragmentTabHostコンポーネントを使用して、この一般的なappホームページの効果を実現します.
まずmainを与えます.xmlファイル

  
  
  
 
 
  
 
   
 
   
 
    
   
 
     

プライマリコード:

 public class MainActivity
 { @ViewInject(android.R.id.tabhost)
   private FragmentTabHost mTabHost;
 private LayoutInflater layoutInflater;
 
 private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3, R.drawable.home_tab4};
   private String mTextviewArray[] = {"  ", "  ", "  ","    "};
   private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class,Fragment4.class};
 
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     init();
   }
 
 @Override
   protected void init() {
 //    list=new JSONArray();
     layoutInflater=LayoutInflater.from(this);
     initTabHost();//       
 }
 
 /**
   *         
   */
   private void initTabHost() {
     mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
     mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     int count = fragmentArray.length;
     for (int i = 0; i < count; i++) {
       TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
           .setIndicator(getTabItemView(i));
       mTabHost.addTab(tabSpec, fragmentArray[i], null);
       mTabHost.getTabWidget().getChildAt(i)
           .setBackgroundResource(R.color.white);
     }
     mTabHost.setCurrentTabByTag(mTextviewArray[0]);
     mTabHost.getTabWidget().setDividerDrawable(null);
 }
 
  /**
   *     
   * @param index    
   * @return    Tab  
   */
   private View getTabItemView(int index) {
     View view = layoutInflater.inflate(R.layout.tab_home_item, null);
     ImageView imageView = (ImageView) view.findViewById(R.id.icon);
     imageView.setImageResource(mImageViewArray[index]);
     TextView textView = (TextView) view.findViewById(R.id.name);
     textView.setText(mTextviewArray[index]);
     return view;
   }
 
 
 
 
 }

以上の文章を通じて、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!