Fragment挂载进Activity

本文介绍了Fragment挂载到Activity的两种方法:一是通过XML布局文件直接声明,二是利用事务动态添加。在XML布局中添加时,需要设置Fragment的name属性,并可预览其内容。另一种方法是在Activity中获取TitleFragment的按钮,通过点击事件动态添加新的Fragment到FrameLayout中。

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

Fragment挂载进Activity中有两个方法

  • 在xml布局文件里直接声明出来
  • 用Fragment的事物动态添加
1.在xml布局文件中添加:

1.1 代码实现:

先把Fragment准备好:

public class TitleFragment extends Fragment implements View.OnClickListener {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //Fragment具体显示的视图
        View view = View.inflate(UiUtils.getContext(), R.layout.xml_title,null);

        return view;
    }

public class ContentFragment extends Fragment {

    private TextView mContent;

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(UiUtils.getContext(), R.layout.xml_content,null);

        return view;
    }

布局文件内容:

  • name属性:代表具体是添加哪一个Fragment,类似于自定义控件时,更改标签
  • tools:属性:使Fragment的内容可以直接显示在andoird studio的阅览窗口
<fragment
            android:layout_centerInParent="true"
            android:id="@+id/fragment_content"                                                                                                                                     android:name="com.itheima.fragmentevent.xml_fragment.ContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/xml_content">
        </fragment>

    <fragment

        android:id="@+id/fragment_title"
        android:name="com.itheima.fragmentevent.xml_fragment.TitleFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/xml_title"
        android:layout_alignParentTop="true"
        >
    </fragment>

1.2具体效果如下:
这里写图片描述

上面四个按钮是TitleFragment
下面的内容是ContentFragment


使用事物动态添加:
  • 在布局文件里声明一个FrameLayout,使用TitleFragment的按钮,动态添加Fragment进FrameLayout
<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"
    tools:context="com.itheima.fragmentevent.MainActivity">


    <!--TitleFragment-->

    <fragment
        android:id="@+id/fragment_title"
        android:name="com.itheima.fragmentevent.xml_fragment.TitleFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/xml_title"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    </fragment>


    <RelativeLayout
        android:layout_below="@id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--ContentFragment-->

        <fragment
            android:layout_centerInParent="true"
            android:id="@+id/fragment_content"
            android:name="com.itheima.fragmentevent.xml_fragment.ContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/xml_content">
        </fragment>

        <!--将动态添加的Fragment覆盖ContentFragment-->

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

        <!--测试Activity和Fragment之间的信息传递-->

        <Button
            android:id="@+id/but_send"
            android:onClick="sendMassage"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="发送消息"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

</RelativeLayout>

2.2在Activity里可以拿到TitleFragment视图对应的View对象,也就可以拿到四个Button,通过Button动态添加新的Fragment

  • 拿到FragmentManager
  • 开启Fragment的事物
  • 将新的Fragment替换占位的FrameLayout
public class MainActivity extends AppCompatActivity  {


    private Button mBut01;
    private Button mBut02;
    private Button mBut03;
    private Button mBut04;
    private FragmentTransaction mTransaction;
    private View view;
    private FragmentManager mFragmentManager;
    private View mContent;
    private Button mSendMassage;


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

        initView();
        initListen();
        initFragment();

    }
    private void initFragment() {
        //拿到Fragment管理器
        mFragmentManager = getSupportFragmentManager();

    }
    private void initListen() {
        mBut01.setOnClickListener(this);
        mBut02.setOnClickListener(this);
        mBut03.setOnClickListener(this);
        mBut04.setOnClickListener(this);
    }

    private void initView() {

        mSendMassage = (Button) findViewById(R.id.but_send);
        view = findViewById(R.id.fragment_title);
        mContent = findViewById(R.id.fragment_content);

        mBut01 = (Button) view.findViewById(R.id.fragment01);
        mBut02 = (Button) view.findViewById(R.id.fragment02);
        mBut03 = (Button) view.findViewById(R.id.fragment03);
        mBut04 = (Button) view.findViewById(R.id.fragment04);
    }


    @Override
    public void onClick(View v) {
        //开启事物
        mTransaction = mFragmentManager.beginTransaction();
        mContent.setVisibility(View.INVISIBLE);
        switch (v.getId()) {
            case R.id.fragment01:
                //通过事物将新的Fragment替换FrameLayout
                mTransaction.replace(R.id.fl_blank,new Fragment01(),"frag01");

                break;
            case R.id.fragment02:
                mTransaction.replace(R.id.fl_blank,new Fragment02());

                break;
            case R.id.fragment03:
                mTransaction.replace(R.id.fl_blank,new Fragment03());

                break;
            case R.id.fragment04:
                mTransaction.replace(R.id.fl_blank,new Fragment04());

                break;
        }

        mTransaction.commit();
    }

效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值