实现选项卡的方式有很多,如何利用FragmentManager和Radiogroup来写一个选项卡呢???
1.我们准备好布局先,activity_main_layout
布局里面准备好fragmentlayout和Radiogroup以及radiobutton
当然,在Radiogroup和radiobutton里需要写好两个适配的xml文件
如下:
<?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="com.example.com.mlsdome.Activity.MainActivity" > <FrameLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_main" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_marginTop="2dp" android:layout_height="70dp" android:background="@color/colorblack" > <RadioGroup android:id="@+id/radioGroup_main" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <RadioButton android:drawableTop="@drawable/match_img_values" android:gravity="center" android:layout_weight="1" android:textColor="@drawable/main_txt_color_values" android:id="@+id/tab1" android:button="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tab1" /> <RadioButton android:drawableTop="@drawable/circle_img_values" android:textColor="@drawable/main_txt_color_values" android:gravity="center" android:layout_weight="1" android:id="@+id/tab2" android:button="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tab2" /> <RadioButton android:drawableTop="@drawable/sport_img_values" android:textColor="@drawable/main_txt_color_values" android:gravity="center" android:layout_weight="1" android:id="@+id/tab3" android:button="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tab3" /> <RadioButton android:textColor="@drawable/main_txt_color_values" android:drawableTop="@drawable/person_img_values" android:gravity="center" android:layout_weight="1" android:id="@+id/tab4" android:button="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tab4" /> </RadioGroup> </LinearLayout> </LinearLayout>
这里是xml适配的图片和文字颜色
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/match_ture" android:state_checked="true"></item> <item android:drawable="@drawable/match_false"></item> </selector>
图片这四个,因为他们基本上是一样的所以在这里我就不一一粘上来了
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorYellow" android:state_checked="true"></item> <item android:color="@color/colorWhite"></item> </selector>
2.我们还需要四个fragment以及他们的布局,如下: (fragment里注意需要一个函数,专门用来创建返回的)
package com.example.com.mlsdome.Activity.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.com.mlsdome.R; /** * Created by 12697 on 2017/3/13. */ /* * * 圈子fragment * */ public class CircleFragment extends Fragment { public static CircleFragment getIntance() { return new CircleFragment(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.circle_fragment_layout,container,false); } }
以及他们的布局,我简单的写了一下他们的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="3" /> </LinearLayout>
3.最后,也是最难的,是主activity里怎么写
分析一下思路:
首先我们定义一些tab1,tab2,tab3,tab4和Radiogroup,以及四个fragment·-----------》
savedInstanceState这个需要注意一下,因为这个是用来存储数据的
也就是说如果fragment里面的数据不为空的时候直接拿来用-----》通过Radiogroup的setOnCheckedChangeListener来对用户的点击进行监听,同时设置第一个为选中状态,
记住这里用的是radiobutton的id来进行选中的,接下来就是Radiogroup里切换事件来
进行对fragment进行监听-----》用到了fragment管理器:fragmentmanager,用他来begin
开启这个事件,当然这里只是判断他的显示,还需要一个函数来对他进行隐藏。实现代码public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private RadioGroup radioGroup; private static final String TAB1="tab1"; private static final String TAB2="tab2"; private static final String TAB3="tab3"; private static final String TAB4="tab4"; private MatchFragment matchFragment; private SportFragment sportFragment; private MineFragment mineFragment; private CircleFragment circleFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { matchFragment = (MatchFragment) getSupportFragmentManager().findFragmentByTag(TAB1); mineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag(TAB2); circleFragment = (CircleFragment) getSupportFragmentManager().findFragmentByTag(TAB3); sportFragment = (SportFragment) getSupportFragmentManager().findFragmentByTag(TAB4); } setContentView(R.layout.activity_main); initView(); } private void initView() { radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup_main); radioGroup.setOnCheckedChangeListener(this); radioGroup.check(R.id.tab1); }@Override public void onCheckedChanged(RadioGroup radioGroup, int i) { FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction(); hideTab(transaction); switch (i) { case R.id.tab1: if (matchFragment == null) { matchFragment = MatchFragment.getIntance(); transaction.add(R.id.fragment_main,matchFragment,TAB1); } else { transaction.show(matchFragment); } break; case R.id.tab2: if (circleFragment == null) { circleFragment = CircleFragment.getIntance(); transaction.add(R.id.fragment_main, circleFragment, TAB2); } else { transaction.show(circleFragment); } break; case R.id.tab3: if (sportFragment == null) { sportFragment = SportFragment.getIntance(); transaction.add(R.id.fragment_main, sportFragment, TAB3); } else { transaction.show(sportFragment); } break; case R.id.tab4: if (mineFragment == null) { mineFragment = MineFragment.getIntance(); transaction.add(R.id.fragment_main, mineFragment, TAB4); } else { transaction.show(mineFragment); } break; } transaction.commit(); }private void hideTab(FragmentTransaction fragmentTransaction) { if (matchFragment != null && matchFragment.isAdded()) { fragmentTransaction.hide(matchFragment); } if (circleFragment != null && circleFragment.isAdded()) { fragmentTransaction.hide(circleFragment); } if (sportFragment != null && sportFragment.isAdded()) { fragmentTransaction.hide(sportFragment); } if (mineFragment != null && mineFragment.isAdded()) { fragmentTransaction.hide(mineFragment); } }
}
Ok,基本上选项卡就这样子实现了,不过,这里的选项卡是有一个弊端的
如果你想改变图片大小的话十分难实现,而且这样会出现各种安卓版本上
的不兼容情况,望慎用。。。。。。。。。。。。。。。。。。