Android 之动态添加Fragment的简单用法

1.创建第一个Test01Fragment,并继承Fragment.

package com.example.a86156.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Test01Fragment extends Fragment {


    public Test01Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_test01, container, false);
        return view;
    }

}

2.fragment_test01.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"
    android:orientation="vertical"
    tools:context=".Test01Fragment">

    <!-- TODO: Update blank fragment layout -->
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="碎片1"
       android:id="@+id/btn1"
       android:layout_gravity="center_horizontal"
       />

</LinearLayout>

3.创建第二个Test02Fragment,并继承Fragment.

package com.example.a86156.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Test02Fragment extends Fragment {


    public Test03Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test02, container, false);
    }

}

4.创建第3个Test03Fragment,并继承Fragment.

package com.example.a86156.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Test03Fragment extends Fragment {


    public Test03Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test03, container, false);
    }

}

5.fragment_test03.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"
    android:background="#00BFFF"
    tools:context=".Test03Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="碎片三" />

</LinearLayout>

6.activity_main.xml布局代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <fragment
        android:id="@+id/test01"
        android:name="com.example.a86156.fragment.Test01Fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/test02"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

7.MainActivity.java代码
package com.example.a86156.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    if(actionBar!=null){
        actionBar.hide();
    }
    showFragment(new Test02Fragment());
    btn1 = findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             showFragment(new Test03Fragment());
        }
    });


}

/**
 *  动态的添加碎片
 * @param fragment 碎片
 */
private void showFragment(Fragment fragment){
    //获取FragmentManager 对象
    FragmentManager manager = getSupportFragmentManager();
    //开启一个事务
    FragmentTransaction transaction = manager.beginTransaction();
    /**
     * test02 容器的ID
     * fragment 待添加的碎片实例
     */
    transaction.replace(R.id.test02,fragment);
    //返回栈
    transaction.addToBackStack(null);
    //提交事务
    transaction.commit();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值