【Android知识点精讲】(3)Fragment详解(一)

本文出处:http://blog.csdn.net/scarthr/article/details/42108273

Fragment是Android3.0之后推出的一个组件,直译为“碎片”,是我个人比较喜欢使用的一个组件。Fragment可以作为Activity的一部分来使用,并且有自己独立的布局和代码,并且可以随时更换Fragment,可以说是非常常用的一个组件。


一 Fragment的生命周期

Fragment的生命周期比较多,总共有11个生命周期:

 

Fragment的第一个生命周期方法是onAttach(),最后一个生命周期方法是onDetach(),其中最重要的是onCreateView方法,在这个方法中会创建Fragment中要显示的View。


二 Fragment与Activity之间交互

Fragment和Activity之间传递数据用Fragment的setArguments方法设置参数值,使用getArguments方法获取参数值。

三 实例

下面我们来看两个例子:
实例一:
我们在主布局中定义两个fragment:
<RelativeLayout 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"
    tools:context="com.thr.fragment.MainActivity$PlaceholderFragment" >

    <fragment
        android:id="@+id/fm_left"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        class="com.thr.fragment.LeftFragment"
        android:text="@string/hello_world" />

    <fragment
        android:id="@+id/fm_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/fm_left"
        class="com.thr.fragment.RightFragment"
        android:text="@string/hello_world" />

</RelativeLayout>
左边的fragment实现了一个ListView,右边的是一个用来展示内容的TextView,注意两个fragment的class参数要指定好我们创建的Fragment类,两个fragment的布局如下:
<RelativeLayout 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"
    tools:context="com.thr.fragment.MainActivity$PlaceholderFragment" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

<RelativeLayout 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"
    tools:context="com.thr.fragment.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="内容区域" />

</RelativeLayout>
两个Fragment的Java文件:
package com.thr.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class LeftFragment extends Fragment implements OnItemClickListener {

	private String[] names = new String[] { "菜单一", "菜单二", "菜单三" };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_left, null);
		ListView listView = (ListView) view.findViewById(R.id.lv);
		listView.setOnItemClickListener(this);
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_list_item_activated_1,
				names);
		listView.setAdapter(arrayAdapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		TextView tv = (TextView) getActivity().findViewById(R.id.tv);
		tv.setText(names[position]);
	}
}

package com.thr.fragment;

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

public class RightFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_right, null);
		return view;
	}
}
我们可以看到在LeftFragment中使用getActivity取得关联它的Activity,再使用findViewById即可获得到RightFragment中的TextView控件。
主Activity没什么特别的:
package com.thr.fragment;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

}
效果图:


可以看出来非常简单吧~

实例二:
我们在主布局放一个FramLayout,在点击按钮后,将这个FramLayout填充为我们自己创建的Fragment,并且发送数据给Fragment,在Fragment中接受数据并显示出来,主要看一下主Activity:
package com.thr.fragment2;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager manager = getFragmentManager();
				FragmentTransaction transaction = manager.beginTransaction();
				MyFragment fragment = new MyFragment();
				Bundle bundle = new Bundle();
				bundle.putString("key", "haha");
				fragment.setArguments(bundle);

				transaction.add(R.id.fl, fragment, "fm");
				transaction.commit();

			}
		});
	}
}
都是比较模式话的操作:
1. 创建FragmentManager对象
2. 然后调用beginTransaction得到FragmentTransaction对象
3. 接着使用add方法,第一个参数传要替换的控件,第二个参数传Fragment,最后一个参数传这个Fragment的Tag。
这个Tag是比较有用的东西,我们在Activity中可以使用getFragmentManager().findFragmentByTag("Tag")来获取我们指定的Fragment。
4. 最后调用commit()方法就可以了,非常简单。
数据的传输用setArguments接受用getArguments。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值