Android基础-3(Fragment)

本文详细介绍了Fragment的基本用法,包括静态Fragment的创建与显示、动态Fragment的切换、Fragment间的数据传递及Fragment的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fragment

1.静态Fragment

  2.动态改变Fragment

  3.Fragment间数据传递

  4.Fragment生命周期

一、静态Fragment

1.新建项目命名为Fragments

2.在res/layout中创建两个布局文件,分别命名为fragment_1、fragment_2

fragment_1.xml:

<?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" 
    android:background="#00ff00">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="25sp"
        android:id="@+id/fragment_1_text"
        android:text="This is fragment1"/>

</LinearLayout>
fragment_2.xml

<?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" 
    android:background="#ffff00">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="25sp"
        android:id="@+id/fragment_2_text"
        android:text="This is fragment2"/>

</LinearLayout>
3.新建两个类继承Fragment(引用android.app.Fragment包),实现onCreateView
OneFragment.class
package com.example.fragments;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OneFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i("tag", "Fra-onCreateView");
		return inflater.inflate(R.layout.fragment_1, container,false);
	}}
TowFragment.class:

package com.example.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TowFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_2, container, false);
	}

}
4.在activity_main.xml中加入两个fragment标签

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.fragments.OneFragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.fragments.TowFragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>
运行如图
二、 动态改变Fragment

1.将activity_main.xml中的fragment注释掉

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <!-- <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.fragments.OneFragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.fragments.TowFragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" /> -->

</LinearLayout>
2.在MainActivity.class中输入如下代码

private LinearLayout activity_main;protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("tag", "Act-onCreate");
		
		activity_main = (LinearLayout) this.findViewById(R.id.activity_main);
		Display display = getWindowManager().getDefaultDisplay();
		FragmentManager fm = getFragmentManager();
		if (display.getWidth() > display.getHeight()) {
			// 横屏
			fm.beginTransaction()
					.replace(R.id.activity_main, new OneFragment()).commit();
		} else {
			// 竖屏
			fm.beginTransaction()
					.replace(R.id.activity_main, new TowFragment()).commit();
		}
	}
OK.试试横竖屏却换看看效果吧!

三、Fragment间数据传递

1.给fragment_2.xml中新增一个按钮

fragment_2.xml:

<?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" 
    android:background="#ffff00">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="25sp"
        android:text="This is fragment2"/>
<Button android:id="@+id/fragment_2_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get fragment_1_text text"/>
</LinearLayout>

2.TowFragment.class中监听按钮事件

TowFragment.class:

package com.example.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TowFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_2, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Button btn = (Button) getActivity().findViewById(R.id.fragment_2_btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				TextView tv= (TextView)getActivity().findViewById(R.id.fragment_1_text);
				Toast.makeText(getActivity(), tv.getText(), 0).show();
			}
		});
	}
}

运行:
取到了fragment1中的TextView文本
四、Fragment生命周期

OK,继续

修改OneFragment.calss

package com.example.fragments;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OneFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i("tag", "Fra-onCreateView");
		return inflater.inflate(R.layout.fragment_1, container,false);
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		Log.i("tag", "Fra-onAttach");
	}
	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		Log.i("tag", "Fra-onDetach");
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i("tag", "Fra-onCreate");
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("tag", "Fra-onDestroy");
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("tag", "Fra-onResume");
	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("tag", "Fra-onPause");
	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("tag", "Fra-onStart");
	}
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("tag", "Fra-onStop");
	}
}

MainActivity.class

package com.example.fragments;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	private LinearLayout activity_main;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("tag", "Act-onCreate");
		
		/*activity_main = (LinearLayout) this.findViewById(R.id.activity_main);
		Display display = getWindowManager().getDefaultDisplay();
		FragmentManager fm = getFragmentManager();
		if (display.getWidth() > display.getHeight()) {
			// 横屏
			fm.beginTransaction()
					.replace(R.id.activity_main, new OneFragment()).commit();
		} else {
			// 竖屏
			fm.beginTransaction()
					.replace(R.id.activity_main, new TowFragment()).commit();
		}*/
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("tag", "Act-onDestroy");
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("tag", "Act-onStart");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("tag", "Act-onStop");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("tag", "Act-onResume");
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("tag", "Act-onPause");
	}
//Act-create -> Fra-Attach -> Fra-create -> Fra-createview -> Act-start -> Fra-start -> Act-Resume -> Fra-Resume
//Fra-pause -> Act-pause -> Fra-stop -> Act-stop -> Fra-Destroy -> Fra-Detach -> Act-Destroy
}

运行:观察LogCat

//Act-create -> Fra-Attach -> Fra-create -> Fra-createview -> Act-start -> Fra-start -> Act-Resume -> Fra-Resume
//Fra-pause -> Act-pause -> Fra-stop -> Act-stop -> Fra-Destroy -> Fra-Detach -> Act-Destroy

附上源码

利用Fragment模仿QQ界面:(具体实现就不写文章了,不懂的地方大家在下面评论吧!我也是新手,每天学习一点点,写博客只是为了巩固今天学的东西,上班没时间,只能在快下班的时候抽点时间学习)


源码:http://download.youkuaiyun.com/detail/q269399361/6913469

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值