Fragment初识(三)

动态添加和删除fragment
这里写图片描述

效果图
这里写图片描述


创建fragment2的代码和布局

public class MyFragment2 extends Fragment {


    public MyFragment2() {
        // 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_my_fragment2, container, false);
    }

}
<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"
             android:background="#ffcacb"
             tools:context="com.feng.viewpagerfragemnt.MyFragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>

</FrameLayout>

这个没什么讲的


mainActivity 的布局

<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:padding="16dp"
              android:orientation="vertical"
              tools:context="com.feng.viewpagerfragemnt.MainActivity">

    <LinearLayout
        android:layout_height="60dp"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:text="添加Fragment"/>

        <Button
            android:id="@+id/button2"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAllCaps="false"
            android:text="删除Fragment"/>
    </LinearLayout>
    <LinearLayout
        android:layout_height="60dp"
        android:layout_width="match_parent"
        android:id="@+id/ll_linearLayout"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

代码

public class MainActivity extends AppCompatActivity {
    private  String TAG="MainActivity";
    private Button button1,button2;
    LinearLayout ll_linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate");
        initView();
        initClick();
    }

    private void initClick() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得fragemnt对象
                FragmentManager fragmentManager= MainActivity.this.getSupportFragmentManager();
                //获得fragmentTransaction
                FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
                //创建MyFragemnt对象
                MyFragment2 myFragment2=new MyFragment2();
                //网指定的布局中添加fragment
                fragmentTransaction.add(R.id.ll_linearLayout,myFragment2,"myFragment2");
                //使fragment生效
                fragmentTransaction.commit();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//获得fragemnt对象
                FragmentManager fragmentManager= MainActivity.this.getSupportFragmentManager();
                //获得fragmentTransaction
                FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
                //通过fragmentManager获得要删除的fragment
                //通过TAg标签寻找到要删除的frgament
                MyFragment2 myFragment2= (MyFragment2) fragmentManager.findFragmentByTag("myFragment2");
                fragmentTransaction.remove(myFragment2);
                //使fragment生效
                fragmentTransaction.commit();
            }
        });
    }

    private void initView() {
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        ll_linearLayout=(LinearLayout)findViewById(R.id.ll_linearLayout);
    }

}

添加fragment

1.获得fragmentManager对象

 FragmentManager fragmentManager= MainActivity.this.getSupportFragmentManager();

2.获得fragmentTransaction对象

FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();

3.创建MyFragemnt对象

  MyFragment2 myFragment2=new MyFragment2();

4.往定的布局中添加fragment

fragmentTransaction.add(R.id.ll_linearLayout,myFragment2,"myFragment2");

5.使fragment生效

fragmentTransaction.commit();

删除fragment
1.获得fragmentManager对象

 FragmentManager fragmentManager= MainActivity.this.getSupportFragmentManager();

2.获得fragmentTransaction

FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();

3.通过fragmentManager获得要删除的fragment
通过TAg标签寻找到要删除的frgament

 MyFragment2 myFragment2= (MyFragment2) fragmentManager.findFragmentByTag("myFragment2");
fragmentTransaction.remove(myFragment2);

4.使fragment生效

  fragmentTransaction.commit();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值