radioGroup+fragment

 
  
//drawable图片改变
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/xing2" android:state_selected="true"/>
    <item android:drawable="@mipmap/xing" android:state_selected="false"/>
selector>
//文字改变
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#03A9F4" android:state_selected="true"/>
    <item android:color="#BFBFC9" android:state_selected="false"/>
selector>

//fragment
ArrayList list= new ArrayList<>();
//radioGroup+fragment
list.add( new Redio_Fragm1())
;list.add( new Redio_Fragm2())
;list.add( new Redio_Fragm3());
//设置默认选中推荐

rb_tuijian
.setSelected( true);
rb_main_duanzi
.setSelected( false);
rb_shipin.setSelected( false);
//得到Fragment的管理器

fragmentManager
= getSupportFragmentManager();
//开启一个事务
final FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
for ( int i = 0; i < list.size(); i++) { beginTransaction.add(R.id. fl,list.get(i));
if(i== 0) { beginTransaction.show(list.get(i)); }
else { beginTransaction.hide(list.get(i)); } }
beginTransaction.commit();


//设置radioGroup点击改变状态
rg
.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i)
{ //开启一个事务
final FragmentTransaction beginTransa = fragmentManager.beginTransaction();
switch (i) {
case R.id. rb_tuijian:
beginTransa.show( list.get( 0));
//点击改变图片和文字
rb_tuijian.setSelected( true);
//别的不改变
rb_main_duanzi.setSelected( false);
rb_shipin.setSelected( false);
beginTransa.hide( list.get( 1));
beginTransa.hide( list.get( 2));
beginTransa.commit();
break;
case
R.id. rb_main_duanzi:
beginTransa.show( list.get( 1));
rb_main_duanzi.setSelected( true);
rb_tuijian.setSelected( false);
rb_shipin.setSelected( false);
beginTransa.hide( list.get( 0));
beginTransa.hide( list.get( 2));
beginTransa.commit();
break;
case R.id. rb_shipin:
beginTransa.show( list.get( 2));
rb_shipin.setSelected( true);
rb_tuijian.setSelected( false);
rb_main_duanzi.setSelected( false);
beginTransa.hide( list.get( 1));
beginTransa.hide( list.get( 0));
beginTransa.commit();
break;
} }});

//布局

 
  
 
  
  <FrameLayout
   android:id="@+id/fl"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   >FrameLayout>


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"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="@dimen/y50"
        android:gravity="center_vertical"
        android:background="#ffffff"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/rb_tuijian"
            android:button="@null"
            android:drawableTop="@drawable/frag_rad_tu"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="推荐"
            android:gravity="center"
            android:textColor="@drawable/frag_rad_wen"
            android:layout_height="@dimen/y40"
            android:padding="@dimen/x5"
            />

        <RadioButton
            android:padding="@dimen/x5"
            android:id="@+id/rb_main_duanzi"
            android:button="@null"
            android:gravity="center"
            android:layout_weight="1"
            android:drawableTop="@drawable/frag_rad_tu2"
            android:layout_width="0dp"
            android:textColor="@drawable/frag_rad_wen"
            android:text="段子"
            android:layout_height="@dimen/y40"
            />

        <RadioButton
            android:padding="@dimen/x5"
            android:id="@+id/rb_shipin"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/frag_rad_tu3"
            android:textColor="@drawable/frag_rad_wen"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="视频"
            android:layout_height="@dimen/y40"
            />
    RadioGroup>

LinearLayout>
 

你可能感兴趣的:(radioGroup+fragment)