关闭

Fragment之Creating event callbacks to activity实例

标签: androidbuttonlayoutencodingclassinterface
3247人阅读 评论(3) 收藏 举报
分类:

在看Android帮助文档的时候,会出现一些例子,但是例子总是穿插着多种知识,让还没有系统学过Android的人读起来很费神难懂,所以就自己写了一个creating event callbacks to activity的例子.

我把Activity的UI分为两个部分,左边和右边,左边用来放置点击的按钮(LeftFragment),右边用来放置对应点击后显示的信息(RightFragment).

Activity的布局layout文件:main.xml

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

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>
LeftFragment的布局layout:leftfragment.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" >

    <Button
        android:id="@+id/first_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/first_button" />

    <Button
        android:id="@+id/second_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/second_button" />

    <Button
        android:id="@+id/third_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/third_button" />

</LinearLayout>
RightFragment的布局layout: rightfragment.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" >

    <TextView
        android:id="@+id/right_show_message"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/holo_orange_dark"
        android:textColor="@android:color/white" />

</LinearLayout>
以上是两个fragment和一个Activity的布局文件,下面来看他们的java文件

Activity: 

public class FirstActivity extends Activity implements MyListener
{
	/**
	 * 实现MyListener,当LeftFragment中点击第一页的时候,让RightFragment显示第一页信息,同理当点击第二页的时候,RightFragment显示第二页信息
	 * 
	 * @param index
	 *            显示的页数
	 */
	public void showMessage(int index)
	{
		if (1 == index)
			showMessageView.setText(R.string.first_page);
		if (2 == index)
			showMessageView.setText(R.string.second_page);
		if (3 == index)
			showMessageView.setText(R.string.third_page);
	}

	/** 得到RightFragment中显示信息的控件 */
	private TextView showMessageView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		System.out.println("Activity--->onCreate");

		FragmentManager manager = getFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		// 动态增加Fragment
		RightFragment rightFragment = new RightFragment();
		LeftFragment leftFragment = new LeftFragment();
		transaction.add(R.id.left_layout, leftFragment, "leftfragment");
		transaction.add(R.id.right_layout, rightFragment, "rightfragment");
		transaction.commit();

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		System.out.println("Activity--->onResume");
		showMessageView = (TextView) findViewById(R.id.right_show_message);
	}
}
LeftFragment:

public class LeftFragment extends Fragment
{
	/** Acitivity要实现这个接口,这样Fragment和Activity就可以共享事件触发的资源了 */
	public interface MyListener
	{
		public void showMessage(int index);
	}

	private MyListener myListener;
	private Button firstButton;
	private Button secondButton;
	private Button thirdButton;

	/** Fragment第一次附属于Activity时调用,在onCreate之前调用 */
	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		System.out.println("LeftFragment--->onAttach");

		myListener = (MyListener) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		System.out.println("LeftFragment--->onCreate");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		System.out.println("LeftFragment--->onCreateView");
		return inflater.inflate(R.layout.leftfragment, container, false);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		System.out.println("LeftFragment--->onResume");

		firstButton = (Button) getActivity().findViewById(R.id.first_button);
		secondButton = (Button) getActivity().findViewById(R.id.second_button);
		thirdButton = (Button) getActivity().findViewById(R.id.third_button);

		MyButtonClickListener clickListener = new MyButtonClickListener();
		firstButton.setOnClickListener(clickListener);
		secondButton.setOnClickListener(clickListener);
		thirdButton.setOnClickListener(clickListener);
	}

	/** 按钮的监听器 */
	class MyButtonClickListener implements OnClickListener
	{
		public void onClick(View v)
		{
			Button button = (Button) v;
			if (button == firstButton)
				myListener.showMessage(1);
			if (button == secondButton)
				myListener.showMessage(2);
			if (button == thirdButton)
				myListener.showMessage(3);
		}
	}
}
RightFragment:

public class RightFragment extends Fragment
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		System.out.println("RightFragment--->onCreate");
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		System.out.println("RightFragment--->onCreateView");
		return inflater.inflate(R.layout.rightfragment, container, false);
	}
}
注意,Fragment的生命周期和Activity生命周期之间的关系。在Activity里动态生成Fragment,首先是Activity调用onCreate()方法,但是这时候还没有加载到Fragment里的组件,当Fragment调用其onCreateView()方法后,Activity才能得到Fragment中的组件.以下可以看到它们的生命周期情况:


图:

这里最关键的就是Fragment要有一个接口和这个接口的引用,而这个接口需要Activity去实现它。当Fragment调用onAttach(Activity acitivity)方法的时候,将这个activity传递给这个接口引用,这样,就可以和Activity进行交互了.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:91866次
    • 积分:791
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:6篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论