Android--点击切换Fragment

本文介绍如何在Android应用中通过点击按钮动态切换显示不同的Fragment。展示了activity的布局设计和Fragment的相关代码实现,实现了点击操作与Fragment显示的交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Fragment:点击不同的按钮,出来不同的Fragment

 

效果图:

 

activity的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="440dp"
        android:id="@+id/layout"
        android:orientation="horizontal"
        >
    </LinearLayout>
    <RadioGroup
        android:layout_below="@id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            >
            <RadioButton
                android:id="@+id/one"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:button="@null"
                android:text="one"
                android:gravity="center"
                android:textSize="22sp"
                android:background="@drawable/back"
                android:layout_height="match_parent" />
            <RadioButton
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:button="@null"
                android:text="two"
                android:gravity="center"
                android:textSize="22sp"
                android:background="@drawable/back"
                android:layout_height="match_parent" />
            <RadioButton
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:button="@null"
                android:text="three"
                android:gravity="center"
                android:background="@drawable/back"
                android:textSize="22sp"
                android:layout_height="match_parent" />
        </LinearLayout>
    </RadioGroup>

</RelativeLayout>

 

activity:

public class MainActivity extends AppCompatActivity {

    private LinearLayout layout;
    private RadioButton one;
    private RadioButton two;
    private RadioButton three;

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

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.layout,new MyFragment1());
        one.setBackgroundResource(R.drawable.back_check);
        transaction.commit();

        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                one.setBackgroundResource(R.drawable.back_check);
                two.setBackgroundResource(R.drawable.back);
                three.setBackgroundResource(R.drawable.back);
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout,new MyFragment1());
                transaction.commit();
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                one.setBackgroundResource(R.drawable.back);
                two.setBackgroundResource(R.drawable.back_check);
                three.setBackgroundResource(R.drawable.back);
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout,new MyFragment2());
                transaction.commit();
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                one.setBackgroundResource(R.drawable.back);
                two.setBackgroundResource(R.drawable.back);
                three.setBackgroundResource(R.drawable.back_check);
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout,new MyFragment3());
                transaction.commit();
            }
        });
    }

    private void initView() {
        layout = (LinearLayout) findViewById(R.id.layout);
        one = (RadioButton) findViewById(R.id.one);
        two = (RadioButton) findViewById(R.id.two);
        three = (RadioButton) findViewById(R.id.three);
    }
}

还有相对应的Fragment所对应的文件

public class MyFragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return View.inflate(getActivity(),R.layout.item1,null);
    }
}

Fragment:布局文件

<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:textSize="24sp"
        android:gravity="center"
        android:id="@+id/tv"
        android:text="one"
        />
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值