android Fragment与Activity交互,互相发数据(附图详解)
来源:互联网 发布:巨鹿之战 知乎 编辑:程序博客网 时间:2024/05/14 15:34
笔者近期看官方training,发现了很多有用又好玩的知识。其中,fragment与Activity通信就是一个。
fragment与Activity通信主要是两点:
1、fragment传递信息给Activity
此点是通过在fragment中定义接口与Activity共享数据。
2、Activity传递信息给fragment
此点主要是通过fragment的getArgument()和setArgument()两个函数传递bundle来传递。
效果:(最后附上源码)
主要流程:
1、在MainActivity中设置一个fragment容器,在MainActivity初始化的时候替换成OneFragment。
2、OneFragment中由于定义了接口,在MainActivity中实现了此接口,当点击button的时候会实现此接口中的函数,并且和Activity共享此数据。(也就是EditText中的内容)
3、MainActivity中获取到数据后,创建一个新的TwoFragment,并且使用Fragment的setArgument()方法把获取到的数据传递到TwoFragment,TwoFragment使用getArgument()接收。
代码:
MainActivity:
- package com.example.trainingfragment;
-
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
-
- public class MainActivity extends Activity
- implements OneFragment.OnOneFragmentClickListener{
-
- private FragmentManager fm;
- private FragmentTransaction ft;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- fm=getFragmentManager();
- ft=fm.beginTransaction();
- ft.replace(R.id.fragment_container,new OneFragment());
-
- ft.commit();
- }
-
- @Override
- public void onArticleClick(String text) {
-
-
- TwoFragment newFragment = new TwoFragment();
- Bundle args=new Bundle();
- args.putString("text", text);
- newFragment.setArguments(args);
-
-
- ft=fm.beginTransaction();
- ft.replace(R.id.fragment_container, newFragment);
-
- ft.commit();
-
- }
- }
OneFragment:
- package com.example.trainingfragment;
-
- 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.Button;
- import android.widget.EditText;
-
-
-
-
-
-
-
-
-
-
- public class OneFragment extends Fragment {
-
- OnOneFragmentClickListener mCallback;
- private EditText et;
- private Button btn;
-
-
- public interface OnOneFragmentClickListener {
-
- void onArticleClick(String text);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view=inflater.inflate(R.layout.fragment_one,container,false);
- et=(EditText)view.findViewById(R.id.et_one);
- btn=(Button)view.findViewById(R.id.btn_one);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mCallback.onArticleClick(et.getText().toString());
- }
- });
-
- return view;
- }
-
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mCallback = (OnOneFragmentClickListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnHeadlineSelectedListener");
- }
- }
- }
TwoFragment:
- package com.example.trainingfragment;
-
- 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 TwoFragment extends Fragment {
-
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-
- return inflater.inflate(R.layout.fragment_two, container, false);
- }
-
- @Override
- public void onStart() {
- super.onStart();
-
- Bundle args=getArguments();
- setMyText(args.getString("text"));
- }
-
- public void setMyText(String text) {
- TextView tv=(TextView)getActivity().findViewById(R.id.tv_two);
- tv.setText(text);
- }
- }
activity_main:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/fragment_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
fragment_one:
- <?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:padding="10dp">
-
- <EditText
- android:id="@+id/et_one"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入要传递的内容" />
-
- <Button
- android:id="@+id/btn_one"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="跳转并传递"
- android:textSize="20sp" />
- </LinearLayout>
fragment_two:
- <?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:padding="10dp">
-
- <EditText
- android:id="@+id/et_one"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入要传递的内容" />
-
- <Button
- android:id="@+id/btn_one"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="跳转并传递"
- android:textSize="20sp" />
- </LinearLayout>
源码地址:http://download.csdn.net/detail/double2hao/9472978
0 0