动态加载Fragment

   Fragment碎片包括静态Fragment和动态Fragment,现在就来介绍一下动态Fragment。

   先来看一下效果:

             


    一.创建第一个Fragment布局

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

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="第一个Fragment"
         android:id="@+id/btn_fragment_one"
         android:layout_gravity="center_horizontal" />
 </LinearLayout>
 
   二.编写第一个Fragment代码
 public class FragmentOne extends Fragment {

     private Button btn;
     @Nullable
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view=inflater.inflate(R.layout.fragment_one,container,false);
         Toast.makeText(getContext(),"第一个碎片",Toast.LENGTH_LONG).show();
         return view;
     }
 }

   三.创建第二个Fragment布局
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="#46EB1C">

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="第二个Fragment"
         android:id="@+id/btn_fragment_two"
         android:layout_gravity="center_horizontal" />
 </LinearLayout>
   四.编写第二个Fragment代码
 public class FragmentTwo extends Fragment {
     @Nullable
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view=inflater.inflate(R.layout.fragment_two,container,false);
         return view;
     }
 }
   五.设置主布局
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="第一个Fragment"
         android:id="@+id/btn_one"
         android:layout_gravity="center_horizontal" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="第二个Fragment"
         android:id="@+id/btn_two"
         android:layout_gravity="center_horizontal" />

     <FrameLayout
         android:id="@+id/frame_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"></FrameLayout>
 </LinearLayout>

   六.编写Activity代码
 public class MainActivity extends AppCompatActivity implements View.OnClickListener{

     private Button btnOne,btnTwo;
     private FragmentManager fragmentManager;
     private Fragment fragmentOne,fragmentTwo,fragmentNow;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_two );
         initView();
         initDefaultFragment();
     }

     private void initDefaultFragment() {
         fragmentOne=new FragmentOne();
         fragmentTwo=new FragmentTwo();
         //碎片管理者
         fragmentManager=getSupportFragmentManager();
         //开启一个事物
         FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
         //add,replace,remove和add集合体,向公共容器中添加一个碎片
         fragmentTransaction.add(R.id.frame_container,fragmentOne);
         //进行提交
         fragmentTransaction.commit();
         fragmentNow=fragmentOne;
     }

     private void initView() {
         btnOne= (Button) findViewById(R.id.btn_one);
         btnTwo= (Button) findViewById(R.id.btn_two);
         btnOne.setOnClickListener(this);
         btnTwo.setOnClickListener(this);
     }

     @Override
     public void onClick(View v) {
         FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
         switch (v.getId()){
             case R.id.btn_one:
                if(fragmentOne.isAdded()){
                    fragmentTransaction.hide(fragmentNow).show(fragmentOne).commit();
                }else {
                    fragmentTransaction.hide(fragmentNow).add(R.id.frame_container,fragmentOne).commit();
                }
                 fragmentNow=fragmentOne;
                 break;
             case R.id.btn_two:
                if(fragmentTwo.isAdded()){
                    fragmentTransaction.hide(fragmentNow).show(fragmentTwo).commit();
                }else {
                    fragmentTransaction.hide(fragmentNow).add(R.id.frame_container,fragmentTwo).commit();
                }
                 fragmentNow=fragmentTwo;
                 break;
         }
     }
 }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值