Fragment

本文详细探讨了Android应用开发中的Fragment组件,包括其生命周期、使用场景、与Activity的交互,以及如何在Fragment间传递数据。通过实例解析,帮助开发者更好地理解和掌握Fragment在实际项目中的运用。
摘要由CSDN通过智能技术生成
package com.example.app5;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.app5.fragment.Context1Fragment;
import com.example.app5.fragment.ContextFragment;

public class MainActivity extends AppCompatActivity {

    private LinearLayout linearLayout;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioGroup radioGroup;
    private Context1Fragment context1Fragment;
    private ContextFragment contextFragment;

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

        linearLayout = findViewById(R.id.ll);
        radioButton1 = findViewById(R.id.rb1);
        radioButton2 = findViewById(R.id.rb2);
        radioGroup = findViewById(R.id.rg1);

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

        contextFragment = new ContextFragment();
        fragmentTransaction.add(R.id.ll,contextFragment);

        context1Fragment = new Context1Fragment();
        fragmentTransaction.add(R.id.ll,context1Fragment);
        fragmentTransaction.hide(context1Fragment);

        fragmentTransaction.commit();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
                if (radioButton.equals(radioButton1)){
                    FragmentManager supportFragmentManager1 = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
//                    ContextFragment contextFragment = new ContextFragment();
//                    fragmentTransaction.replace(R.id.ll,contextFragment);
                    fragmentTransaction1.hide(context1Fragment);
                    fragmentTransaction1.show(contextFragment);
                    fragmentTransaction1.commit();

                }
                if (radioButton.equals(radioButton2)){
                    FragmentManager supportFragmentManager2 = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction2 = supportFragmentManager2.beginTransaction();
                    fragmentTransaction2.hide(contextFragment);
                    fragmentTransaction2.show(context1Fragment);
                    fragmentTransaction2.commit();
                }
            }
        });


    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="9">

<!--        <androidx.viewpager.widget.ViewPager-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="match_parent">-->

<!--        </androidx.viewpager.widget.ViewPager>-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">

        <RadioGroup
            android:id="@+id/rg1"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="match_parent">

            <RadioButton
                android:id="@+id/rb1"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:button="@null"

                android:gravity="center"
                android:text="One"
                />
            <RadioButton
                android:id="@+id/rb2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:button="@null"
                android:text="Two"
                />

        </RadioGroup>



    </LinearLayout>




</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment.Context1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Two"></Button>



</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment.Context1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Two"></Button>



</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.ContextFragment">

    <!-- TODO: Update blank fragment layout -->
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="One" />

</FrameLayout>


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.app5.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContextFragment extends Fragment {

    private Button button;

    public ContextFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_context, container, false);

        button = inflate.findViewById(R.id.b1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "One", Toast.LENGTH_SHORT).show();
            }
        });

        return inflate;
    }

}```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值