Fragment的使用(四)

上一篇说到了activity与fragment之间的数据互传 , 但是是在动态加载fragment中实现的。


这一篇就说下载静态加载fragment中怎么样实现数据互传 。。


首先看下main_activity的布局页面:

fragment是用来静态加载fragment用的,这个应该知道了。下面的textView和button是用来获取和显示fragment传过来的值用的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment  
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.fourthfragment.MyFragment"/>
    <TextView 
        android:id="@+id/mainText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mainActivity的TextView"
        />
	<Button 
	    android:id="@+id/mainButton"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="mainActivity的button"
	    />
</LinearLayout>


再看下Fragment中对用的布局文件:

这里的TextView和button是用来获取和显示activity传过来的值用的。

<?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/fragText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FragmentText"
        />
    <Button 
        android:id="@+id/fragButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取activity传递的数据"
        />
</LinearLayout>

在看Fragment文件:

package com.example.fourthfragment;

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;
/**
 * Fragment
 * @author lolli
 *
 */
public class MyFragment extends Fragment{

	//  数据
	private String data;
	//	定义控件
	private TextView textView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment, container, false);
		textView = (TextView) view.findViewById(R.id.fragText);
		Button button = (Button) view.findViewById(R.id.fragButton);
		// 点击button获取值
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 获取activity传递的数据并设置到fragment里面的textView中
				String text = getData();
				textView.setText(text);
			}
		});
		
		
		// 设置传递给activity的值
		setData("这是fragment传送给activity的数据.....");
		return view;
	}
	/**
	 * 获取数据
	 * @return
	 */
	public String getData() {
		return data;
	}
	/**
	 * 设置数据
	 * @param data
	 */
	public void setData(String data) {
		this.data = data;
	}
}

前面动态加载的时候我们创建了一个接口来让activity实现并获取数据。

这里我们添加了一个data属性,并给了get/set方法。这种法师感觉是简单多了。

这里说一下具体的实现步骤:

1. Fragment中添加data属性,给get/set方法

2. 在MainActivity中给date的set方法中set数据,Fragment中通过get方法获取activity中set的数据。这样就拿到了activity传过来的数据。

3. 在Fragment给data的set方法中set数据,Activity中通过getData拿到fragment中set的数据,这样activity就拿到了fragment中传过来的数据。


实现思路已经说清楚了,那就直接上代码好了:

package com.example.fourthfragment;

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

public class MainActivity extends Activity {

	// 定义控件
	private Button button;
	private TextView textView;
	private MyFragment myFragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化控件
		button = (Button) findViewById(R.id.mainButton);
		textView = (TextView) findViewById(R.id.mainText);
		
		// 获取fragmentManager
		FragmentManager fragmentManager = getFragmentManager();
		// 通过fragmentManager找到fragment
		myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment);
		// 向 MyFragment中的data 设置数据
		myFragment.setData("这是activity传送的数据 ....");
		// 点击button接收fragment传递过来的值
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String date = myFragment.getData();
				textView.setText(date);
			}
		});
		
	}
}

这里我们获取到fragmentManager并不像动态加载那样去打开事务,再去add方法添加fragment。

我们拿到fragmentManager,然后通过findFragmentById获取到静态加载的fragment,拿到后就直接操作data了。


这样我们就实现了静态加载fragment中的数据交互。微笑


Fragment相关的简单介绍结束了,涉及的东西比较多,也比较不好理解,只有多思考多敲,才能机智的使用Fragment,而且发现fragment已经成为官方主推的了,相信以后将会大量采用Fragment的方式,而不是纯activity。微笑












发布了53 篇原创文章 · 获赞 4 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章