Android中的Fragment使用详解之页面加载

本文详细介绍了Android中Fragment的静态加载与动态加载方法,包括布局文件创建、类实现、Activity整合与操作,以及动态加载时的事务管理与界面切换。

静态加载

1、Fragment布局

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
2、编写我的MyFragment

package com.cx.testdemo;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MyFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// layout文件转化为View对象(需要加载的布局文件,加载layout的父ViewGroup,是否返回父窗体ViewGroup)
		View view = inflater.inflate(R.layout.fragment, container, false);
		TextView textView = (TextView) view.findViewById(R.id.textView1);
		textView.setText("静态加载");
		
		return view;
	}
}
注意:在引入jar包时要引入import android.app.Fragment;,不能引入import android.support.v4.app.Fragment;。引入后者时,代码不会报错,但运行时报错。
3、在Activity的布局中使用MyFragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <fragment 
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.cx.testdemo.MyFragment"/>

</LinearLayout>
注意:一定要加id属性,虽然在其他地方用不到。

4、在Activity中使用

package com.cx.testdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity2 extends Activity {
	private TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		
		//直接使用fragment布局文件中的Button。静态加载特点,在Activity中对Fragment中的控件进行改变。
		Button button = (Button) findViewById(R.id.button1);
		textView = (TextView) findViewById(R.id.textView1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textView.setText("改变");
			}
		});
	}
}

动态加载

动态加载的前两步与静态加载相同,这里直接从第三步开始
3、Activity的布局文件中将上面3中布局的fragment换成LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>
4、在Activity中动态加载fragment
package com.cx.testdemo;
/*
 * 动态加载
 */
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity2 extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		
		MyFragment2 fragment2 = new MyFragment2();
		//获取Fragment管理者
		FragmentManager fragmentManager = getFragmentManager();
		//通过Fragment管理者开启一个事务的对象
		FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
		//(添加到的布局id,Fragment)
		beginTransaction.add(R.id.frame, fragment2);
		//返回键回退效果
		beginTransaction.addToBackStack(null);
		beginTransaction.commit();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c小旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值