效果如图:
使用Fragment实现tab的缺点就是不能够滑动。不过应该也算优点,具体场景可以自由选择。
完整代码:imooc-tab022fragment,在我的百度云网盘上。
MainAcgtivity.java
1 package com.example.imooc_tab02; 2 3 import android.os.Bundle; 4 import android.support.v4.app.FragmentActivity; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentTransaction; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.ImageButton; 10 import android.widget.LinearLayout; 11 12 public class MainActivity extends FragmentActivity implements OnClickListener { 13 // tabs 14 LinearLayout tab_weixin, tab_frd, tab_addr, tab_settings; 15 // ImageButton 16 ImageButton btn_weixin, btn_py, btn_txl, btn_sz; 17 18 tab01 t1; 19 tab02 t2; 20 tab03 t3; 21 tab04 t4; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.main); 27 t1 = new tab01(); 28 t2 = new tab02(); 29 t3 = new tab03(); 30 t4 = new tab04(); 31 initView(); 32 initEvents(); 33 34 /* 35 * 把tab添加到FrameLayout中,transaction的前一个参数是FrameLayout 36 * 的id,第二个参数是Fragment的继承类的实例对象 37 */ 38 // transaction.add(R.id.id_fragment, t1); 39 40 } 41 42 private void initEvents() { 43 tab_weixin.setOnClickListener(this); 44 tab_frd.setOnClickListener(this); 45 tab_addr.setOnClickListener(this); 46 tab_settings.setOnClickListener(this); 47 tab_addr.callOnClick();//设置通讯录为默认点击, 48 } 49 50 private void initView() { 51 // TODO Auto-generated method stub 52 tab_weixin = (LinearLayout) findViewById(R.id.id_tab_weixin2); 53 // tab_frd,tab_addr,tab_settings 54 tab_frd = (LinearLayout) findViewById(R.id.id_tab_frd2); 55 tab_addr = (LinearLayout) findViewById(R.id.id_tab_address2); 56 tab_settings = (LinearLayout) findViewById(R.id.id_tab_settings2); 57 58 // btn_weixin,btn_py,btn_txl,btn_sz; 59 btn_weixin = (ImageButton) findViewById(R.id.id_imgbtn_wx); 60 btn_py = (ImageButton) findViewById(R.id.id_imgbtn_py); 61 btn_txl = (ImageButton) findViewById(R.id.id_imgbtn_txl); 62 btn_sz = (ImageButton) findViewById(R.id.id_imgbtn_sz); 63 } 64 65 @Override 66 public void onClick(View v) { 67 // TODO Auto-generated method stub 68 FragmentManager manager = getSupportFragmentManager(); 69 FragmentTransaction transaction = manager.beginTransaction(); 70 hideFragment(transaction); 71 switch (v.getId()) { 72 case R.id.id_tab_weixin2: 73 resetImg(); 74 if(!t1.isAdded()){ 75 //如果fragment对象t1没有被添加,那么就添加并显示 76 transaction.add(R.id.id_fragment, t1); 77 } 78 transaction.show(t1); 79 btn_weixin.setImageResource(R.drawable.tab_weixin_pressed); 80 break; 81 case R.id.id_tab_frd2: 82 resetImg(); 83 if (!t2.isAdded()) { 84 transaction.add(R.id.id_fragment, t2); 85 } 86 transaction.show(t2); 87 btn_py.setImageResource(R.drawable.tab_find_frd_pressed); 88 break; 89 case R.id.id_tab_address2: 90 resetImg(); 91 if (!t3.isAdded()) { 92 transaction.add(R.id.id_fragment, t3); 93 } 94 transaction.show(t3); 95 btn_txl.setImageResource(R.drawable.tab_address_pressed); 96 break; 97 case R.id.id_tab_settings2: 98 resetImg(); 99 if (!t4.isAdded()) { 100 transaction.add(R.id.id_fragment, t4); 101 } 102 transaction.show(t4); 103 btn_sz.setImageResource(R.drawable.tab_settings_pressed); 104 break; 105 106 default: 107 break; 108 } 109 transaction.commit(); 110 } 111 112 private void hideFragment(FragmentTransaction transaction) { 113 System.out.println("====开始hideFragment()...===="); 114 // TODO Auto-generated method stub 115 if (t1 != null) { 116 System.out.println("hide(t1)..."); 117 transaction.hide(t1); 118 } 119 if (t2 != null) { 120 System.out.println("hide(t2)..."); 121 transaction.hide(t2); 122 } 123 if (t3 != null) { 124 System.out.println("hide(t3)..."); 125 transaction.hide(t3); 126 } 127 if (t4 != null) { 128 System.out.println("hide(t4)..."); 129 transaction.hide(t4); 130 } 131 } 132 133 // 重置按钮为灰色的 134 public void resetImg() { 135 btn_weixin.setImageResource(R.drawable.tab_weixin_normal); 136 btn_py.setImageResource(R.drawable.tab_find_frd_normal); 137 btn_txl.setImageResource(R.drawable.tab_address_normal); 138 btn_sz.setImageResource(R.drawable.tab_settings_normal); 139 } 140 }