fragment使用replace和RadioGroup布局

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Button btn1;
Button btn2;
Button btn3;
Button btn4;
RadioGroup group;

FragmentManager supportFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    group = findViewById(R.id.group);
     btn1 = findViewById(R.id.btn1);
    btn2 = findViewById(R.id.btn2);
    btn3 = findViewById(R.id.btn3);
    btn4 = findViewById(R.id.btn4);
    Fram1 fram1=new Fram1();
     supportFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fram,fram1);
    fragmentTransaction.commit();

    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i){
                case R.id.btn1:
                    FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fram,new Fram1());
                    fragmentTransaction.commit();
                    break;
                case R.id.btn2:
                    FragmentTransaction fragmentTransaction2 = supportFragmentManager.beginTransaction();
                    fragmentTransaction2.replace(R.id.fram,new Fram2());
                    fragmentTransaction2.commit();

                    break;  case R.id.btn3:
                    FragmentTransaction fragmentTransaction3 = supportFragmentManager.beginTransaction();
                    fragmentTransaction3.replace(R.id.fram,new Fram3());
                    fragmentTransaction3.commit();

                    break;  case R.id.btn4:
                    FragmentTransaction fragmentTransaction4 = supportFragmentManager.beginTransaction();
                    fragmentTransaction4.replace(R.id.fram,new Fram4());
                    fragmentTransaction4.commit();
                    break;


            }
        }
    });
}

}

<?xml version="1.0" encoding="utf-8"?>

    <FrameLayout
        android:id="@+id/fram"
        android:layout_width="match_parent"
        android:layout_weight="9"
        android:layout_height="0dp"></FrameLayout>
<RadioGroup
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center"
        android:button="@null"
        android:checked="true"
        android:background="@drawable/dise"
        android:text="影片"/>
    <RadioButton
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center"
        android:button="@null"
        android:background="@drawable/dise"
        android:text="影院"/>

    <RadioButton
    android:id="@+id/btn3"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:gravity="center"
    android:button="@null"
        android:background="@drawable/dise"
    android:text="我的"/>
    <RadioButton
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/dise"
        android:button="@null"
        android:text="设置"/>

</RadioGroup>

猜你喜欢

转载自blog.csdn.net/LI_YUANQI/article/details/83035273