动态添加和删除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();