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; } } }