Android之Fragment的动态添加

Fragment

Fragment是Android3.0后引入的一个新的API,我们可以把它看成看成一个小型的Activity,又称Activity片段。Fragment有自己的生命周期但它仍需要嵌套在Activity 中使用,并且会受到Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁。

在Activity中添加Fragment有两种方式,一种是静态添加,一种是动态添加。

接下来主要讲一下动态添加:

1、创建三个fragment.xml布局文件,分别为:first_fragment.xml、second_fragment.xml、third_fragment.xml,代码如下:

first_fragment.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00ff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="This is the first fragment"/>

</LinearLayout>

second_fragment.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="This is the second fragment"/>

</LinearLayout>

third_fragment.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FFA500"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="This is the third fragment"/>

</LinearLayout>

2、创建fragment布局文件对应的继承Fragment类的java类,FirstFragment.java、SecondFragment.java、ThirdFragment.java,代码如下:

public class FirstFragment extends Fragment {

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.first_fragment, container,false);
		return view;
	}
	
}

SecondFragment.java和ThirdFragment.java是同理的。

3、在主布局文件activity_main.xml中,声明三个按钮和一个帧布局控件,按钮用来控制添加fragment,帧布局用来存放fragment。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:orientation="vertical">
        
        <Button
	         android:id="@+id/tofirst"
	         android:layout_width="wrap_content"
	         android:layout_height="wrap_content"
	         android:layout_gravity="center"
	         android:text="第一个fragment"/>
	    <Button
	         android:id="@+id/tosecond"
	         android:layout_width="wrap_content"
	         android:layout_height="wrap_content"
	         android:layout_gravity="center"
	         android:text="第二个fragment"/>
	    <Button
	         android:id="@+id/tothird"
	         android:layout_width="wrap_content"
	         android:layout_height="wrap_content"
	         android:layout_gravity="center"
	         android:text="第三个fragment"/> 
    </LinearLayout>
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2">
    </FrameLayout>
        
</LinearLayout>

4、在主布局文件对应的MainActivity.java类中将fragment添加到帧布局FrameLayout中,代码:

public class MainActivity extends Activity {
	Button btn1,btn2,btn3,btn3_2,btn2_1;
	FragmentManager fragmentManager;
	FragmentTransaction fragmentTransaction;
	FirstFragment rightFragment;
	SecondFragment anotherrightFragment;
	ThirdFragment thirdfragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1 = (Button)findViewById(R.id.tofirst);
        btn2 = (Button)findViewById(R.id.tosecond);
        btn3 = (Button)findViewById(R.id.tothird);
    }
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		btn1.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi") @Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*在activity对应java类中通过getFragmentManager()
				*获得FragmentManager,用于管理ViewGrop中的fragment
				* */
				fragmentManager=getFragmentManager();
				/*FragmentManager要管理fragment(添加,替换以及其他的执行动作)
				*的一系列的事务变化,需要通过fragmentTransaction来操作执行
				*/
				fragmentTransaction = fragmentManager.beginTransaction();
				//实例化要管理的fragment
				rightFragment = new FirstFragment();
				//通过添加(事务处理的方式)将fragment加到对应的布局中
				fragmentTransaction.add(R.id.right_layout, rightFragment);
				//事务处理完需要提交
				fragmentTransaction.commit();
			}
		});
		btn2.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi") public void onClick(View v) {
				// TODO Auto-generated method stub
				/*在activity对应java类中通过getFragmentManager()
				*获得FragmentManager,用于管理ViewGrop中的fragment
				* */
				fragmentManager=getFragmentManager();
				/*FragmentManager要管理fragment(添加,替换以及其他的执行动作)
				*的一系列的事务变化,需要通过fragmentTransaction来操作执行
				*/
				fragmentTransaction = fragmentManager.beginTransaction();
				//实例化要管理的fragment
				anotherrightFragment = new SecondFragment();
				//通过添加(事务处理的方式)将fragment加到对应的布局中
				fragmentTransaction.add(R.id.right_layout, anotherrightFragment);
				//事务处理完需要提交
				fragmentTransaction.commit();
			}
		});
		btn3.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi") @Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				fragmentManager = getFragmentManager();
				fragmentTransaction = fragmentManager.beginTransaction();
				thirdfragment = new ThirdFragment();
				fragmentTransaction.add(R.id.right_layout, thirdfragment);
				fragmentTransaction.commit();
			}
		});
	}
}
  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值