您的位置:首页 > 其它

Fragment的使用(三)

2016-03-13 20:59 162 查看
静态加载Fragment,动态加载Fragment都学习了,不过写得比较片面,只是一个简单的用法,实际情况可能会复杂一些,但会用了再拓展一下应该不是很难。

纯粹的加载页面布局了,当然是不够了,这一节我们学习一下activity与fragment之间的数据互传。

这一节内容也不好理解,建议是边看边理解,然后再敲,再理解。

首页看看main_activity的布局页面:

嵌套linearLauyout是为加载fragment用的, 下面的输入框个按钮的目的是:我们点击按钮,获取输入的内容,然后传给fragment.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

</LinearLayout>
<EditText
android:id="@+id/editText"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendButton"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"
/>
</LinearLayout>


接下来看看MainActivity的处理:

先我们不看实现的接口,也不看接口实现的back方法 。

首先我们给布局文件中的按钮设置点击事件,在点击事件中,我们获取到了文本框输入的值,并动态加载了fragment

在动态加载中我们创建了一个bundle对象,我们将获取到的输入的值put到bundle对象中,然后再用setArgument将bundle对象放到fragment中,最后我们提交这个fragment事务,这样我们就将值传到了我们加载的fragment中了。

package com.example.thirdfragment;

import com.example.thirdfragment.Myfragment.BackListener;

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;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements BackListener{

//  定义控件
private EditText editText;
private Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//  初始化控件
editText = (EditText) findViewById(R.id.editText);
sendButton = (Button) findViewById(R.id.sendButton);
// 设置发送按钮的点击事件
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 获取文本框输入的数据
String text = editText.getText().toString();
// 获取fragment对象
Myfragment fragment = new Myfragment();
// 创建一个bundle对象
Bundle bundle = new Bundle();
// 给值到创建的bundle对象
bundle.putString("text", text);
//  将带值的bundle对象放入fragment中
fragment.setArguments(bundle);
// 动态加载fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main_layout, fragment);
fragmentTransaction.commit();

}
});
}

// 接口实现方法
@Override
public void back(String data) {
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
}


activity已经将值传到了fragment中,我们再看看fragment中怎么样获取值的:

首先,我们只看onCreateView方法中的内容。

我们直接getArgument获取了一个bundle对象,然后bundle对象直接get方法通过传入的时候放的key获取到值。

这样就实现了activity向fragment传值,并在fragment中接收值 。

然后我们看看fragment中怎么向activity中传值呢?

package com.example.thirdfragment;

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

public class Myfragment extends Fragment{

private String backData = "这是fragment返回的字符串";

private BackListener backListener;
public interface BackListener{
public void back(String data);
};

@Override
public void onAttach(Activity activity) {
backListener = (BackListener) activity;
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment, container, false);

//  接受activity传递到当前fragment的值
Bundle bundle = getArguments();
String text = bundle.get("text").toString();
// 将接收的值设置到页面中
TextView textView  = (TextView) view.findViewById(R.id.fragmentText);
textView.setText(text);

// 给接口设置返回给activity的值
backListener.back(backData);
return view;

}

}


我们建议是:在Fragment中定义一个内部回调接口,再让activity实现该接口,我们通过接口将值传给activity,并让activity获取到值。

具体的实现完美可以参考代码:

1. 首先定义并初始化了回调的数据:

<span>	</span>private String backData = "这是fragment返回的字符串";


2. 然后再定义了一个内部接口,并定义了一个接口中的一个回调方法,并给该接口定义了一个对象

<span>	</span>private BackListener backListener;
<span>	</span>public interface BackListener{
public void back(String data);
};
3. 接下来重写onAttach方法,将fragment被加载到activity中的时候将会执行,把activity强转成我们写的内部接口并赋值给该接口。

<span>	</span>@Override
public void onAttach(Activity activity) {
backListener = (BackListener) activity;
super.onAttach(activity);
}
4. 我们将要回调的数据set到接口要回调的方法中

<span>	</span>// 给接口设置返回给activity的值
backListener.back(backData);


通过以上四步呢我们就实现了frament想activity设值。。

那么activity怎么取值呢?

我们再回过头去看看MainActivity.

1. 首先实现了在fragment中创建的内部接口BackListener

2. 实现了接口中的back方法,那么参数中的data就是fragment传给activity的值了 。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: