网上实现Tab功能的方法有很多,这里我使用Fragment的方法,我觉着比较简单易懂
MainActivity
1 private android.app.FragmentManager fragmentManager; 2 3 private FragmentOne fragmentOne; 4 private FragmentTwo fragmentTwo; 5 private FragmentThree fragmentThree; 6 private FragmentFour fragmentFour; 7 8 private RelativeLayout layoutOne, layoutTwo, layoutThree, layoutFour; 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContent(R.layout.activity_main); 14 initViews(); 15 fragmentManager = getFragmentManager(); 16 setTabSelection(0); 17 } 18 19 private void initViews(){ 20 layoutOne = (RelativeLayout) findViewById(R.id.layoutOne); 21 layoutOne.setOnClickListener(this); 22 //init所有的layout,并设置监听器 23 } 24 25 @Override 26 public void onClick(View v){ 27 switch(v.getId()){ 28 case R.id.layoutOne: 29 setTabSelection(0); 30 //case 其余所有tab 31 default:break; 32 } 33 } 34 35 /** 36 * 根据传入index参数来设置选中的tab页 37 * @param index 38 * 39 */ 40 private void setTabSelection(int index){ 41 clearSelection(); 42 //开启fragment事物 43 FragmentTransaction transaction = fragmentManager.beginTransaction(); 44 hideFragments(transaction); 45 switch (index){ 46 //FragmentOne 47 case 0: 48 if(fragmentMain == null){ 49 fragmentOne = new FragmentOne(); 50 transaction.add(R.id.content, fragmentOne); 51 }else { 52 transaction.show(fragmentOne); 53 } 54 clearSelection(); 55 //set text color and image resource 56 break; 57 //case 其余所有 58 } 59 transaction.commit(); 60 } 61 62 /** 63 * 清除所有选中状态 64 * 65 */ 66 private void clearSelection(){ 67 //set text color and image resource to default 68 } 69 70 /** 71 * 将所有fragment设置为隐藏状态 72 * @param transaction 73 * 用于对Fragment执行操作的事务 74 */ 75 private void hideFragments(FragmentTransaction transaction){ 76 if(fragmentOne != null){ 77 transaction.hide(fragmentOne); 78 } 79 if(fragmentTwo != null){ 80 transaction.hide(fragmentTwo); 81 } 82 if(fragmentThree != null){ 83 transaction.hide(fragmentThree); 84 } 85 if(fragmentFour != null){ 86 transaction.hide(fragmentFour); 87 } 88 }
activity_main.xml 注意:这里bottom为最下边的四个tab的布局,里边有上面代码中的layoutOne,layoutTwo,layoutThree,layoutFour
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="match_parent" android:layout_height="@dimen/public_line_height" android:background="@color/background"/> <include android:id="@+id/layout_bottom" layout="@layout/bottom" android:layout_width="fill_parent" android:layout_height="@dimen/bottom_height" android:layout_alignParentBottom="true"/> <FrameLayout android:id="@+id/content" android:layout_above="@id/layout_bottom" android:layout_alignBottom="@id/layout_bottom" android:background="@color/background" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/bottom_height"/> </RelativeLayout> </LinearLayout>
四个Fragment直接继承Fragment就好
需要传递activity时
1 private Context context; 2 private View view; 3 4 @Nullable 5 @Override 6 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 7 View mainLayout = inflater.inflate(R.layout.fragment_main, container, false); 8 context = this.getActivity(); 9 view = mainLayout; 10 initView(); 11 return view; 12 }
这样就可以通过context来进行操作了