欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android动态添加碎片

程序员文章站 2022-05-31 22:04:53
我们编写一个能够用过按钮动态更替碎片的APP,首先在主页上显示第一个碎片,点击按钮后可以替换到第二个碎片,或者删除已经替换掉的第二个碎片。 一.MainActivity.java import androidx.fragment.app.FragmentActivity; import androi ......

我们编写一个能够用过按钮动态更替碎片的app,首先在主页上显示第一个碎片,点击按钮后可以替换到第二个碎片,或者删除已经替换掉的第二个碎片。

一.mainactivity.java

import androidx.fragment.app.fragmentactivity;
import androidx.fragment.app.fragmentmanager;
import androidx.fragment.app.fragmenttransaction;

import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;

public class mainactivity extends fragmentactivity {

    public mainactivity() {
        log.e("tag", "mainactivity()..");
    }

    @override
    protected void oncreate(bundle savedinstancestate) {
        log.e("tag", "mainactivity oncreate()..");
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);//重写oncreate()方法


        // 创建fragment对象
        final myfragment1 fragment1 = new myfragment1();
        // 得到fragmentmanager
        fragmentmanager manager = getsupportfragmentmanager();
        // 得到fragmenttransacation
        fragmenttransaction transaction = manager.begintransaction();
        // 添加fragment对象并提交
        transaction.add(r.id.ll_main, fragment1).commit();

        button button1=(button)findviewbyid(r.id.fragment_1);
        button1.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                showfragment2();
            }
        });
        button button2=(button)findviewbyid(r.id.fragment_2);
        button2.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                deletefragment2();
            }
        });

    }
    private myfragment2 fragment2;
    public void showfragment2() {
        // 创建fragment对象
        fragment2 = new myfragment2();
        // 得到fragmentmanager
        fragmentmanager manager = getsupportfragmentmanager();
        // 得到fragmenttransacation
        fragmenttransaction transaction = manager.begintransaction();

        //将当前操作添加到回退栈, 这样点击back回到上一个状态
        transaction.addtobackstack(null);

        // 替换fragment对象并提交
        transaction.replace(r.id.ll_main, fragment2).commit();
    }
    public void deletefragment2() {

        // 得到fragmentmanager
        fragmentmanager manager = getsupportfragmentmanager();
        // 得到fragmenttransacation
        fragmenttransaction transaction = manager.begintransaction();
        // 移除fragment对象并提交
        transaction.remove(fragment2).commit();
    }
}

二.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">
    <linearlayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<button

    android:id="@+id/fragment_1"
    android:text="切换至第二个碎片"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content" />
    <button
        android:id="@+id/fragment_2"
        android:text="删除第二个碎片"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    </linearlayout>
<linearlayout
    android:id="@+id/ll_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</linearlayout>
</linearlayout>

编写好的界面如下图所示:
Android动态添加碎片

 

 

三.myfragment1.java

import android.os.bundle;

import androidx.fragment.app.fragment;

import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.textview;


public class myfragment1 extends fragment {



    public myfragment1() {
        // required empty public constructor
    }



    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);

    }

    @override
    public view oncreateview(layoutinflater inflater, viewgroup container,
                             bundle savedinstancestate) {
        // inflate the layout for this fragment
        return inflater.inflate(r.layout.fragment_my_fragment1, container,false);
    }


}

四.myfragment2.java

import android.os.bundle;

import androidx.fragment.app.fragment;

import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.textview;



public class myfragment2 extends fragment {
    public myfragment2() {
        // required empty public constructor
    }



    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);

    }

    @override
    public view oncreateview(layoutinflater inflater, viewgroup container,
                             bundle savedinstancestate) {
        // inflate the layout for this fragment
        return inflater.inflate(r.layout.fragment_my_fragment2, container,false);
    }

}

五.fragment1.xml

<?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=".myfragment1">

    <!-- todo: update blank fragment layout -->
    <textview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是第一个碎片" />

</framelayout>

六.fragment2.xml

<?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=".myfragment2">

    <!-- todo: update blank fragment layout -->
    <textview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是第二个碎片" />

</framelayout>

完毕