首页 > 代码库 > Fragment + TabHost + RadioGroup

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法,

Demo 效果图:


先上布局XML R.layout.activity_main

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@android:id/tabhost"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <LinearLayout  
  8.         android:orientation="vertical"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent">  
  11.   
  12.         <TabWidget   
  13.             android:id="@android:id/tabs"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_weight="0.0"  
  17.             android:visibility="gone"/>  
  18.           
  19.             <include android:id="@+id/header"  
  20.                 layout="@layout/my_header"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_width="fill_parent"/>  
  23.                       
  24.         <FrameLayout   
  25.             android:id="@android:id/tabcontent"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="0.0dip"  
  28.             android:layout_weight="1.0">  
  29.       
  30.             <fragment   
  31.                 android:name="com.example.coolsmile.fragment.HomeFragment"  
  32.                 android:id="@+id/HomeFragment"  
  33.                 android:layout_width="match_parent"  
  34.                 android:layout_height="match_parent"  
  35.                 />  
  36.             <fragment   
  37.                 android:name="com.example.coolsmile.fragment.fragment_tab2"  
  38.                 android:id="@+id/fragment_tab2"  
  39.                 android:layout_width="match_parent"  
  40.                 android:layout_height="match_parent"  
  41.                 />  
  42.             <fragment   
  43.                 android:id="@+id/fragment_tab3"  
  44.                 android:name="com.example.coolsmile.fragment.fragment_tab3"  
  45.                 android:layout_width="match_parent"  
  46.                 android:layout_height="match_parent"  
  47.                 />  
  48.             <fragment   
  49.                 android:id="@+id/fragment_tab4"  
  50.                 android:name="com.example.coolsmile.fragment.fragment_tab4"  
  51.                 android:layout_width="match_parent"  
  52.                 android:layout_height="match_parent"  
  53.                 />  
  54.             <fragment   
  55.                 android:id="@+id/fragment_tab5"  
  56.                 android:name="com.example.coolsmile.fragment.fragment_tab5"  
  57.                 android:layout_width="match_parent"  
  58.                 android:layout_height="match_parent"  
  59.                 />  
  60.         </FrameLayout>  
  61.                
  62.             <RadioGroup  
  63.                 android:id="@+id/main_tab"  
  64.                 android:background="@drawable/bottom1"  
  65.                 android:orientation="horizontal"  
  66.                 android:layout_width="fill_parent"  
  67.                 android:layout_height="wrap_content"  
  68.                 android:gravity="center_vertical"  
  69.                 android:layout_gravity="bottom">  
  70.       
  71.                 <RadioButton  
  72.                     android:id="@+id/main_tab_home"    
  73.                     style="@style/MMTabButton"    
  74.                     android:layout_weight="1.0"        
  75.                     android:drawableTop="@drawable/menu_icon_0_normal"    
  76.                     android:text="@string/main_home"/>  
  77.                 <RadioButton  
  78.                     android:id="@+id/main_tab_info"  
  79.                     style="@style/MMTabButton"  
  80.                     android:layout_weight="1.0"  
  81.                     android:drawableTop="@drawable/menu_icon_1_normal"  
  82.                     android:text="@string/main_my_info" />  
  83.                 <RadioButton  
  84.                     android:id="@+id/main_tab_news"  
  85.                     style="@style/MMTabButton"  
  86.                     android:layout_weight="1.0"  
  87.                     android:drawableTop="@drawable/menu_icon_2_normal"  
  88.                     android:text="@string/main_news" />  
  89.                 <RadioButton   
  90.                     android:id="@+id/main_tab_search"    
  91.                     style="@style/MMTabButton"    
  92.                     android:layout_weight="1.0"    
  93.                     android:drawableTop="@drawable/menu_icon_3_normal"    
  94.                     android:text="@string/main_search"/>  
  95.                 <RadioButton  
  96.                     android:id="@+id/main_tab_settings"  
  97.                     style="@style/MMTabButton"  
  98.                     android:layout_weight="1.0"  
  99.                     android:drawableTop="@drawable/menu_icon_3_normal"  
  100.                     android:focusable="false"  
  101.                     android:text="@string/main_settings" />  
  102.   
  103.             </RadioGroup>  
  104.                       
  105.   
  106.             <TextView  
  107.                 android:id="@+id/main_tab_new_message"  
  108.                 android:layout_width="wrap_content"  
  109.                 android:layout_height="wrap_content"  
  110.                 android:layout_gravity="center_horizontal|top"  
  111.                 android:layout_marginLeft="15dip"  
  112.                 android:layout_marginTop="-46dip"  
  113.                 android:background="@drawable/tips"  
  114.                 android:gravity="center"  
  115.                 android:textColor="#ffffff"  
  116.                 android:textSize="10sp"  
  117.                 android:visibility="visible" />  
  118.     
  119.     </LinearLayout>  
  120. </TabHost>  

这里有个Fragment标签 

android:name="com.example.coolsmile.fragment.HomeFragment"

这里的android:name="具体的Fragment的实现类"


这里我的Acitivty继承了FargmentActivity,onCreate方法里

[java] view plaincopy
  1. setContentView(R.layout.activity_main);  
  2. InitUI();   

[java] view plaincopy
  1. private void InitUI(){  
  2.   
  3. // get Resource R.string  
  4. InitUIString();  
  5. // set Message Number  
  6. TextView main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);  
  7. main_tab_new_message.setVisibility(View.VISIBLE);  
  8. main_tab_new_message.setText("6");  
  9.   
  10.   
  11. tabHost = (TabHost) findViewById(android.R.id.tabhost);  
  12. tabHost.setup();  
  13.   
  14. tabHost.addTab(  
  15. tabHost.newTabSpec(main_home).setIndicator(main_home).setContent(R.id.HomeFragment)  
  16. );  
  17. tabHost.addTab(  
  18. tabHost.newTabSpec(main_my_info).setIndicator(main_my_info).setContent(R.id.fragment_tab2)  
  19. );  
  20. tabHost.addTab(  
  21. tabHost.newTabSpec(main_news).setIndicator(main_news).setContent(R.id.fragment_tab3)  
  22. );  
  23. tabHost.addTab(  
  24. tabHost.newTabSpec(main_search).setIndicator(main_search).setContent(R.id.fragment_tab4)  
  25. );  
  26. tabHost.addTab(  
  27. tabHost.newTabSpec(main_settings).setIndicator(main_settings).setContent(R.id.fragment_tab5)  
  28. );  
  29.   
  30. tabHost.setCurrentTab(0);  
  31.   
  32. InitClickListener();  
  33. }  
  34.   
  35. private void InitClickListener(){  
  36.   
  37. RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab);  
  38. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  39.   
  40. @Override  
  41. public void onCheckedChanged(RadioGroup group, int checkedId) {  
  42. // TODO Auto-generated method stub  
  43. switch (checkedId) {  
  44. case R.id.main_tab_home:  
  45. tabHost.setCurrentTabByTag(main_home);  
  46. break;  
  47. case R.id.main_tab_info:  
  48. tabHost.setCurrentTabByTag(main_my_info);  
  49. break;  
  50. case R.id.main_tab_news:  
  51. tabHost.setCurrentTabByTag(main_news);  
  52. break;  
  53. case R.id.main_tab_search:  
  54. tabHost.setCurrentTabByTag(main_search);  
  55. break;  
  56. case R.id.main_tab_settings:  
  57. tabHost.setCurrentTabByTag(main_settings);  
  58. break;  
  59. default:  
  60. tabHost.setCurrentTabByTag(main_home);  
  61. }  
  62. }  
  63. });  
  64. }  
  65. @Override  
  66. public void onClick(View v) {  
  67.   
  68.   
  69. }  
  70. public MainHeader getHeader(){  
  71. return this.header;  
  72. }  
  73. private void InitUIString(){  
  74.   
  75. main_home = getResources().getString(R.string.main_home);  
  76. main_my_info = getResources().getString(R.string.main_my_info);  
  77. main_news = getResources().getString(R.string.main_news);  
  78. main_search = getResources().getString(R.string.main_search);  
  79. main_settings = getResources().getString(R.string.main_settings);  
  80. }  

这里实例了一个效果图的HomeFragMent

[java] view plaincopy
  1. public class HomeFragment extends Fragment{  
  2.   
  3.     View view;  
  4.    
  5.     String[] presidents = {   
  6.                 "Dwight D. Eisenhower",   
  7.                 "John F. Kennedy",   
  8.                 "Lyndon B. Johnson",   
  9.                 "Richard Nixon",   
  10.                 "Gerald Ford",   
  11.                 "Jimmy Carter",   
  12.                 "Ronald Reagan",   
  13.                 "George H. W. Bush",   
  14.                 "Bill Clinton",   
  15.                 "George W. Bush",   
  16.                 "Barack Obama"   
  17.             };   
  18.        
  19.     @Override  
  20.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  21.             Bundle savedInstanceState) {  
  22.                 // TODO Auto-generated method stub  
  23.                 view = inflater.inflate(R.layout.fragment_tab1,container,false);  
  24.                 Init();  
  25.                 return view ;  
  26.     }  
  27.       
  28.     protected void Init(){  
  29.                   
  30.         ListView list = (ListView)view.findViewById(android.R.id.list);  
  31.           
  32.         list.setAdapter(new ArrayAdapter<String>(getActivity(),   
  33.                 android.R.layout.simple_list_item_1, presidents));  
  34.         list.setOnItemClickListener(listener);  
  35.     }  
  36.       
  37.     private OnItemClickListener listener = new OnItemClickListener() {  
  38.   
  39.         @Override  
  40.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  41.                 long id) {  
  42.             // TODO Auto-generated method stub  
  43.             Intent intent = new Intent(getActivity(), ListContainActivity.class);  
  44.               
  45.             intent.putExtra("PRESIDENTS_CONTENT",presidents[position]);  
  46.   
  47.             getActivity().startActivity(intent);      
  48.         }     
  49.     };  
  50.       
  51. }  


点击: 下载DEMO





Fragment + TabHost + RadioGroup