FragmentTabHost in Android is simple to use

FragmentTabHost

If fragmentTabHost is used in AndroidStudio, you don't need to import any package. If you use it in eclipse, you need to import a package support-v4-19.0.0.jar. The path is in

E:\androidSDK\android-sdk_r24.4.1-windows\android-sdkwindows\extras\android\m2repository\com\android\support\support-v4\19.0.0


use:  

Fragment is divided into up and down button distribution



The above format, the upper and lower formats are all fixed

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:id="@+id/fragmentContentId">
   </FrameLayout>

   <android.support.v4.app.FragmentTabHost
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/fragmentTabHostId">
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
         <TabWidget
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@android:id/tabs">
<!--The Id here must be the same as the Id inside android-->
</TabWidget>
         <FrameLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@android:id/tabcontent">

         </FrameLayout>

      </LinearLayout>
   </android.support.v4.app.FragmentTabHost>

</LinearLayout>

lower layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:id="@+id/fragmentContentId">
   </FrameLayout>

   <android.support.v4.app.FragmentTabHost
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/fragmentTabHostId">
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
         <FrameLayout
             android:layout_width="0dp"
             android:layout_height="0dp"
             android:id="@android:id/tabcontent">

         </FrameLayout>

      </LinearLayout>
   </android.support.v4.app.FragmentTabHost>

</LinearLayout>



Initialize in code






package com.liwangjiang.fragmentTabHostName;

import com.liwangjiang.fragmentClass.OneFragment;
import com.liwangjiang.fragmentClass.ThereFragment;
import com.liwangjiang.fragmentClass.TwoFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends FragmentActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		//1. Find the Fragment object
		FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		//2. Initialize this FragmentTabHost
		//containertId is the container in which Fragment will be displayed in my dream
		tabHost.setup(this,getSupportFragmentManager() , android.R.id.tabcontent);//初始化FragmentTabHost
		
		//3. Add tabs to each FragmentTagHost
		TabHost.TabSpec one=tabHost.newTabSpec("1");//This character is the identifier representing this tab
		one.setIndicator("One");//Represents the displayed content
	
		TabHost.TabSpec two=tabHost.newTabSpec("2");//This character is the identifier representing this tab
		one.setIndicator("two");//Represents the displayed content
		
		TabHost.TabSpec there=tabHost.newTabSpec("3");//This character is the identifier representing this tab
		one.setIndicator("there");//Represents the displayed content
		// set the receiver
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				Log.v("liwangjiang", ""+tabId);
			}
		});
		
		
		// method to set the current page
		//Set according to the corner label of the label
		tabHost.setCurrentTab(1);//Indicates to set the current display page 0-1-2 page 1
		//Set according to TabSpecTag
		tabHost.setCurrentTabByTag("2");
		
		
		tabHost.addTab(one,OneFragment.class,null);
		
		tabHost.addTab(two,TwoFragment.class,null);
		
		tabHost.addTab(there,ThereFragment.class,null);
	}
}
  private void initFragmentTabHost(){
        //find FragmentTabHost
        FragmentTabHost tabHost=findViewById(R.id.fragmentTabHostId);
        //Initialize FragmentTabHost
        tabHost.setup(this,getSupportFragmentManager(),R.id.fragmentContentId);
        String[] titles=getResources().getStringArray(R.array.tab_title);
        int[] imageViewResource = new  int[]{R.drawable.news_selector,R.drawable.reading_selector,R.drawable.video_selector,R.drawable.topic_selector,R.drawable.mine_selector};
        Class[] clz = new Class[]{NewFragment.class, EmptyFragment.class,NewFragment.class ,EmptyFragment.class, EmptyFragment.class};
        for (int i=0;i<titles.length;i++){
            TabHost.TabSpec tmp=tabHost.newTabSpec(""+i);
            tmp.setIndicator(getEmptyView(this,titles,imageViewResource,i));
            tabHost.addTab(tmp, clz[i],null);
        }

        //Add options for each Fragment
       // TabHost.TabSpec one=tabHost.newTabSpec("0");
        //one.setIndicator("one");//Displayed content
        //one.setIndicator(getEmptyView(this));

    }
    private View getEmptyView(Context context,String[] title,int[] imageViews,int index){
        LayoutInflater inflater = LayoutInflater.from(context);
        View view=inflater.inflate(R.layout.item_title,null);
        //set text
        TextView textView=view.findViewById(R.id.tv_textViewId);
        textView.setText(title[index]);
        //set image
        ImageView imageView=view.findViewById(R.id.iv_ImageView);
        imageView.setImageResource(imageViews[index]);
        return view;
    }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325871205&siteId=291194637