Fragment动态添加+android笔记2

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
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{
  Button btn2,btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }
//implements View.OnClickListener这个接口不要忘记了
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn2:
                replacefragment(new BlankFragment1());
                break
                ;
            case R.id.btn3: break;
        }
    }

    /**
     * 这个函数用于在Android开发中切换Fragment。
     * 它通过获取FragmentManager和FragmentTransaction对象,
     * 使用replace方法将当前的Fragment替换为新的BlankFragment1实例,并提交该事务。
     * @param v
     */
     public void replacefragment(Fragment fragment ){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1,fragment );//替换
        fragmentTransaction.addToBackStack(null);//添加到后退栈
        fragmentTransaction.commit();//提交事务
    }


}
fragmentTransaction.addToBackStack(null);//添加到后退栈,相当于在栈里面叠加;
<FrameLayout
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:id="@+id/fragment1"
    android:background="#97E188C3"
    ></FrameLayout>这个布局不要写错了
<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="按钮1"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="按钮2"
        />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/fragment1"
        android:background="#97E188C3"
        ></FrameLayout>

</LinearLayout>
package com.example.fragment;

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.TextView;

/**
 * A simple {@link Fragment} subclass.
 * Use the  factory method to
 * create an instance of this fragment.
 */
public class BlankFragment1 extends Fragment {
    /**
     *下面是主动生成的代码,但暂时不需要用到,可以删掉
     */
//    // TODO: Rename parameter arguments, choose names that match
//    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
//    private static final String ARG_PARAM1 = "param1";
//    private static final String ARG_PARAM2 = "param2";
//
//    // TODO: Rename and change types of parameters
//    private String mParam1;
//    private String mParam2;
//
//    public BlankFragment1() {
//        // Required empty public constructor
//    }
//
//    /**
//     * Use this factory method to create a new instance of
//     * this fragment using the provided parameters.
//     *
//     * @param param1 Parameter 1.
//     * @param param2 Parameter 2.
//     * @return A new instance of fragment BlankFragment1.
//     */
//    // TODO: Rename and change types and number of parameters
//    public static BlankFragment1 newInstance(String param1, String param2) {
//        BlankFragment1 fragment = new BlankFragment1();
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
//        return fragment;
//    }

    private  View root;
    public TextView tv;
    public Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(root==null){
             root=inflater.inflate((R.layout.fragment_blank1),container, false);
        }
        tv=root.findViewById(R.id.tv);
        btn=root.findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener(){


            @Override
            public void onClick(View view) {
                tv.setText("Hello");
            }
        });
        return root;
    }
}

这个是fragment.xml 

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

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:id="@+id/tv"
        android:text="@string/hello_blank_fragment" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/btn1"
        android:text="按钮"/>

</LinearLayout>

相关推荐

  1. Android动态添加 Fragment

    2024-04-02 20:34:05       60 阅读
  2. Android Fragment 生命周期

    2024-04-02 20:34:05       60 阅读
  3. Android基础-Fragment详解

    2024-04-02 20:34:05       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 20:34:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 20:34:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 20:34:05       82 阅读
  4. Python语言-面向对象

    2024-04-02 20:34:05       91 阅读

热门阅读

  1. FastAPI Web框架教程 第11章 请求响应的进阶用法

    2024-04-02 20:34:05       31 阅读
  2. 蓝桥杯刷题记录之质数

    2024-04-02 20:34:05       35 阅读
  3. nignx的功能包括哪些

    2024-04-02 20:34:05       35 阅读
  4. LeetCode226.翻转二叉树

    2024-04-02 20:34:05       39 阅读
  5. 洛谷 B3918 [语言月赛 202401] 图像变换

    2024-04-02 20:34:05       36 阅读
  6. Ubuntu设置中文输入法教程

    2024-04-02 20:34:05       38 阅读
  7. 题目 1567: 超级玛丽

    2024-04-02 20:34:05       37 阅读