Fragment+RadioGroup点击切换Fragment

来源:互联网 发布:机房机柜网络整改 编辑:程序博客网 时间:2024/06/05 08:36
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup rg;
    private RadioButton[] rbArray;
    private ArrayList<Fragment> listFragemnt;
    private FragmentManager fm;
    private int count=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initview();
    }


    private void initview() {
        rg = (RadioGroup) findViewById(R.id.rg);
        rbArray = new RadioButton[rg.getChildCount()];
        for (int i = 0; i <rbArray.length ; i++) {
            rbArray[i] = (RadioButton) rg.getChildAt(i);
        }
        fm = getSupportFragmentManager();
       listFragemnt = new ArrayList<>();
        FragmentA fma = new FragmentA();
        FragmentB fb = new FragmentB();
        FragmentC fc = new FragmentC();


        listFragemnt.add(fma);
        listFragemnt.add(fb);
        listFragemnt.add(fc);


        FragmentTransaction tt = fm.beginTransaction();
        tt.add(R.id.layout,listFragemnt.get(0));
        tt.commit();
        rbArray[0].setChecked(true);
        rg.setOnCheckedChangeListener(this);


    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        showFragment(i);
    }


    private void showFragment(int j) {
        FragmentTransaction tt = fm.beginTransaction();
        for (int i = 0; i <rbArray.length ; i++) {
            if(rbArray[i].getId()== j){
                if(listFragemnt.get(i).isAdded()){
                    tt.show(listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
                }else {
                    tt.add(R.id.layout,listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
                }
                count = i;
                break;
            }


        }


    }


}


布局


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"    android:layout_width="match_parent" android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.app3.MainActivity"><LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/layout"    android:orientation="vertical"    ></LinearLayout>    <RadioGroup        android:layout_width="match_parent"        android:layout_height="30dp"        android:orientation="horizontal"        android:id="@+id/rg"        android:layout_alignParentBottom="true">        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:id="@+id/rb1"            android:layout_weight="1"            android:gravity="center"            android:text="内容1"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:id="@+id/rb2"            android:layout_weight="1"            android:gravity="center"            android:text="内容2"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:id="@+id/rb3"            android:layout_weight="1"            android:gravity="center"            android:text="内容3"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@null"            android:id="@+id/rb4"            android:layout_weight="1"            android:gravity="center"            android:text="内容4"            />

原创粉丝点击