zoukankan      html  css  js  c++  java
  • 动态添加Fragment

    在Fragment简单用法的基础上做修改

    一.新建:another_right_fragment.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#0000ff">

        <TextView

            android:text="另一个raight fragment"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/textView3"

            android:layout_weight="1" />

    </LinearLayout>

    二.新建AnotherRightFragment类:

    public class AnotherRightFragment extends Fragment {

        @Override

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

            View view = inflater.inflate(R.layout.another_right_fragment, container, false);

            return view;

        }

    }

    三.修改activity_main.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">

        <fragment

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:name="com.example.guo.fragment.LeftFragment"

            android:layout_weight="1"

            android:id="@+id/left_fragment" />

        <fragment

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:name="com.example.guo.fragment.RightFragment"

            android:layout_weight="1"

            android:id="@+id/right_fragment" />

    </LinearLayout>

    四.修改left_fragment.xml

    给按钮增加android:onClick="myClick"

    五.修改MainActivity,增加click方法,

         protected void myClick(View view){

            if( view.getId() == R.id.btn ){

                AnotherRightFragment anotherRightFragment = new AnotherRightFragment();

                FragmentManager fragmentManager = getFragmentManager();

                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                fragmentTransaction.replace(R.id.right_fragment, anotherRightFragment);

                fragmentTransaction.addToBackStack(null); // 按下返回键,返回上一个Fragment;不加改行,则直接退出应用

                fragmentTransaction.commit();

            }

        }

    可以看到,首先我们给左侧碎片中的按钮注册了一个点击事件,然后将动态添加碎片的

    逻辑都放在了点击事件里进行。结合代码可以看出,动态添加碎片主要分为5 步。

    1. 创建待添加的碎片实例。

    2. 获取到FragmentManager,在活动中可以直接调用getFragmentManager()

    方法得到。

    3. 开启一个事务,通过调用beginTransaction()方法开启。

    4. 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id 和待添加的

    碎片实例。

    5. 提交事务,调用commit()方法来完成。

  • 相关阅读:
    SpringBoot校验(validation)
    序列化/反序列化
    全面的整理了原生js
    apache commons工具类简介
    刚从git上download的代码,有个工具类中某个类找不到
    Hadoop(三)手把手教你搭建Hadoop全分布式集群
    Hadoop(一)之初识大数据与Hadoop
    Hadoop(二)搭建伪分布式集群
    Git(一)之基本操作详解
    Git(二)Git几个区的关系与Git和GitHub的关联
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6732466.html
Copyright © 2011-2022 走看看