4月13日Fragment动态添加与管理

在线性布局中LinearLayout中有两个Button需要显示出来
并且这种形式
则需要在线性布局的大环境中加入方向
android:orientation="vertical"
显示效果
在这里插入图片描述
activity_main.xml文件

<?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">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/change" ></Button>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/replace"></Button>

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/purple_200"></FrameLayout>


</LinearLayout>

MainActivity.java文件

package com.example.fragmentmanagers;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{          //点击事件的两种方式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(this);    //传入this,默认面向对象的原则,会自动触发onClick函数


        Button button2 = findViewById(R.id.btn2);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){       //根据不同id点击,显示不同Button
            case R.id.btn:          //此处为冒号
                replaceFragment(new BlankFragment1());//创建一个函数
                break;
            case R.id.btn2:
                replaceFragment(new ItemFragment());//又要new一个Fragment
        }
    }

    //动态切换frahment
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();    //获取默认管理类
        FragmentTransaction transation = fragmentManager.beginTransaction();
        transation.replace(R.id.framelayout,fragment);
        //像activity一样有栈,添加到栈中,点击返回键,有复现
        //transation.addToBackStack(null);
        transation.commit();//有管理站
    }
}

其他文件自动生成
BlankFragment1.java
ItemFragment.java
还会自动生成
xml

运行效果
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值