Android_Fragment实现Tab

一.布局

          

 

          

 

二.代码

1.先写布局文件

主界面的布局文件  主要是分两部分:上面是一个实现Fragment中的内容,下面是RadioGroup

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/ll"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentBottom="true"  
  12.         android:background="#006644" >  
  13.   
  14.         <RadioGroup  
  15.             android:id="@+id/rg"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="wrap_content"  
  18.             android:orientation="horizontal" >  
  19.   
  20.             <RadioButton  
  21.                 android:id="@+id/rb_message"  
  22.                 android:layout_width="0dp"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:layout_weight="1"  
  25.                 android:button="@null"  
  26.                 android:checked="true"  
  27.                 android:drawableTop="@android:drawable/btn_star"  
  28.                 android:gravity="center"  
  29.                 android:text="消息" />  
  30.   
  31.             <RadioButton  
  32.                 android:id="@+id/rb_contact"  
  33.                 android:layout_width="0dp"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:layout_weight="1"  
  36.                 android:button="@null"  
  37.                 android:drawableTop="@android:drawable/btn_star"  
  38.                 android:gravity="center"  
  39.                 android:text="联系人" />  
  40.   
  41.             <RadioButton  
  42.                 android:id="@+id/rb_dynamic"  
  43.                 android:layout_width="0dp"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:layout_weight="1"  
  46.                 android:button="@null"  
  47.                 android:drawableTop="@android:drawable/btn_star"  
  48.                 android:gravity="center"  
  49.                 android:text="动态" />  
  50.   
  51.             <RadioButton  
  52.                 android:id="@+id/rb_setting"  
  53.                 android:layout_width="0dp"  
  54.                 android:layout_height="wrap_content"  
  55.                 android:layout_weight="1"  
  56.                 android:button="@null"  
  57.                 android:drawableTop="@android:drawable/btn_star"  
  58.                 android:gravity="center"  
  59.                 android:text="设置" />  
  60.         </RadioGroup>  
  61.     </LinearLayout>  
  62.   
  63.     <FrameLayout  
  64.         android:id="@+id/fl_content"  
  65.         android:layout_width="match_parent"  
  66.         android:layout_height="match_parent"  
  67.         android:layout_above="@id/ll" >  
  68.     </FrameLayout>  
  69.   
  70. </RelativeLayout>  


四个Fragment中的布局文件

 

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_centerInParent="true"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <ImageView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_gravity="center_horizontal"  
  16.             android:src="@android:drawable/btn_star" />  
  17.   
  18.         <TextView  
  19.             android:id="@+id/tv_content"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_gravity="center_horizontal"  
  23.             android:padding="10dp"  
  24.             android:textSize="20sp" />  
  25.     </LinearLayout>  
  26.   
  27. </RelativeLayout>  


写四个Fragment来加载对应的布局

 

 

[html] view plain copy
 
  1. package com.example.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class Fragment_Contact extends Fragment {  
  11.   
  12.     private TextView tv_content;  
  13.   
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  16.             Bundle savedInstanceState) {  
  17.   
  18.         View view = inflater.inflate(R.layout.fragment_main, null);  
  19.         tv_content = (TextView) view.findViewById(R.id.tv_content);  
  20.         tv_content.setText("联系人");  
  21.   
  22.         return view;  
  23.     }  
  24.   
  25. }  

 

[html] view plain copy
 
  1. package com.example.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class Fragment_Dynamic extends Fragment {  
  11.   
  12.     private TextView tv_content;  
  13.   
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  16.             Bundle savedInstanceState) {  
  17.   
  18.         View view = inflater.inflate(R.layout.fragment_main, null);  
  19.         tv_content = (TextView) view.findViewById(R.id.tv_content);  
  20.         tv_content.setText("动态");  
  21.   
  22.         return view;  
  23.     }  
  24. }  

 

[java] view plain copy
 
  1. package com.example.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class Fragment_message extends Fragment {  
  11.   
  12.     private TextView tv_content;  
  13.   
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  16.             Bundle savedInstanceState) {  
  17.   
  18.         View view = inflater.inflate(R.layout.fragment_main, null);  
  19.         tv_content = (TextView) view.findViewById(R.id.tv_content);  
  20.         tv_content.setText("消息");  
  21.   
  22.         return view;  
  23.     }  
  24.       
  25.       
  26.   
  27. }  

 

 

[java] view plain copy
 
  1. package com.example.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class Fragment_Setting extends Fragment {  
  11.   
  12.     private TextView tv_content;  
  13.   
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  16.             Bundle savedInstanceState) {  
  17.   
  18.         View view = inflater.inflate(R.layout.fragment_main, null);  
  19.         tv_content = (TextView) view.findViewById(R.id.tv_content);  
  20.         tv_content.setText("设置");  
  21.   
  22.         return view;  
  23.     }  
  24.       
  25.       
  26.   
  27. }  

 

主界面中实现Tab的切换

 

[java] view plain copy
 
    1. package com.example.fragment;  
    2.   
    3. import android.app.Activity;  
    4. import android.app.FragmentManager;  
    5. import android.app.FragmentTransaction;  
    6. import android.os.Bundle;  
    7. import android.util.Log;  
    8. import android.view.Window;  
    9. import android.widget.FrameLayout;  
    10. import android.widget.RadioButton;  
    11. import android.widget.RadioGroup;  
    12. import android.widget.RadioGroup.OnCheckedChangeListener;  
    13.   
    14. public class MainActivity extends Activity {  
    15.   
    16.     protected static final String TAG = "MainActivity";  
    17.     private RadioGroup rg;  
    18.     private RadioButton rb_message;  
    19.     private RadioButton rb_contact;  
    20.     private RadioButton rb_dynamic;  
    21.     private RadioButton rb_setting;  
    22.     private FrameLayout fl_content;  
    23.     private Fragment_message fragment_message;  
    24.     private Fragment_Contact fragment_contact;  
    25.     private Fragment_Dynamic fragment_dynamic;  
    26.     private Fragment_Setting fragment_setting;  
    27.   
    28.     private FragmentManager fragmentManager; // 管理Fragment  
    29.   
    30.     @Override  
    31.     protected void onCreate(Bundle savedInstanceState) {  
    32.         super.onCreate(savedInstanceState);  
    33.   
    34.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
    35.         setContentView(R.layout.activity_main);  
    36.   
    37.         init();  
    38.   
    39.         // 得到一个Fragment的管理者  
    40.         fragmentManager = getFragmentManager();  
    41.         // 开启一个Fragment事务  
    42.         FragmentTransaction transaction = fragmentManager.beginTransaction();  
    43.         fragment_message = new Fragment_message();  
    44.         transaction.add(R.id.fl_content, fragment_message);  
    45.         transaction.commit();  
    46.   
    47.         Log.i(TAG, "消息的Fragment创建成功");  
    48.   
    49.         // RadioGroup的点击事件  
    50.         rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
    51.   
    52.             // transaction = fragmentManager.beginTransaction();  
    53.             // hideFragments(transaction);  
    54.   
    55.             @Override  
    56.             public void onCheckedChanged(RadioGroup arg0, int arg1) {  
    57.   
    58.                 // 开启一个Fragment事务  
    59.                 FragmentTransaction transaction = fragmentManager.beginTransaction();  
    60.                 hideFragments(transaction);  
    61.   
    62.                 if (arg1 == rb_message.getId()) {  
    63.                     if (fragment_message == null) {  
    64.                         fragment_message = new Fragment_message();  
    65.                         transaction.add(R.id.fl_content, fragment_message);  
    66.                     } else {  
    67.                         transaction.show(fragment_message);  
    68.                     }  
    69.   
    70.                     Log.i(TAG, "选中了消息的按钮");  
    71.   
    72.                 } else if (arg1 == rb_contact.getId()) {  
    73.                     if (fragment_contact == null) {  
    74.                         fragment_contact = new Fragment_Contact();  
    75.                         transaction.add(R.id.fl_content, fragment_contact);  
    76.                     } else {  
    77.                         transaction.show(fragment_contact);  
    78.                     }  
    79.   
    80.                     Log.i(TAG, "选中了联系人的按钮");  
    81.   
    82.                 } else if (arg1 == rb_dynamic.getId()) {  
    83.                     if (fragment_dynamic == null) {  
    84.                         fragment_dynamic = new Fragment_Dynamic();  
    85.                         transaction.add(R.id.fl_content, fragment_dynamic);  
    86.                     } else {  
    87.                         transaction.show(fragment_dynamic);  
    88.                     }  
    89.   
    90.                     Log.i(TAG, "选中了动态的按钮");  
    91.   
    92.                 } else if (arg1 == rb_setting.getId()) {  
    93.                     if (fragment_setting == null) {  
    94.                         fragment_setting = new Fragment_Setting();  
    95.                         transaction.add(R.id.fl_content, fragment_setting);  
    96.                     } else {  
    97.                         transaction.show(fragment_setting);  
    98.                     }  
    99.   
    100.                     Log.i(TAG, "选中了设置的按钮");  
    101.   
    102.                 }  
    103.                 transaction.commit();  
    104.             }  
    105.         });  
    106.   
    107.     }  
    108.   
    109.     /** 
    110.      * 控件的实例化 
    111.      */  
    112.     private void init() {  
    113.   
    114.         rg = (RadioGroup) findViewById(R.id.rg);  
    115.         rb_message = (RadioButton) findViewById(R.id.rb_message);  
    116.         rb_contact = (RadioButton) findViewById(R.id.rb_contact);  
    117.         rb_dynamic = (RadioButton) findViewById(R.id.rb_dynamic);  
    118.         rb_setting = (RadioButton) findViewById(R.id.rb_setting);  
    119.         fl_content = (FrameLayout) findViewById(R.id.fl_content);  
    120.   
    121.     }  
    122.   
    123.     /** 
    124.      * 将所有的Fragment都置为隐藏状态。 
    125.      *  
    126.      * @param transaction 
    127.      *            用于对Fragment执行操作的事务 
    128.      */  
    129.     private void hideFragments(FragmentTransaction transaction) {  
    130.   
    131.         if (fragment_message != null) {  
    132.             transaction.hide(fragment_message);  
    133.         }  
    134.         if (fragment_contact != null) {  
    135.             transaction.hide(fragment_contact);  
    136.         }  
    137.         if (fragment_dynamic != null) {  
    138.             transaction.hide(fragment_dynamic);  
    139.         }  
    140.         if (fragment_setting != null) {  
    141.             transaction.hide(fragment_setting);  
    142.         }  
    143.   
    144.     }  
    145.   
    146. }  
posted @ 2017-04-18 08:38  天涯海角路  阅读(90)  评论(0)    收藏  举报