Android-Fragment(与Activity 直接传参 / 通信)

来源:互联网 发布:用友t3数据备份 编辑:程序博客网 时间:2024/06/10 14:54

1.回顾

 上篇学习了 Fragment的生命周期 相关知识

2.重点

   (1)Fragment基本知识

   (2)Fragment 静态加载

   (3)Fragment 动态加载

   (4)Fragment的生命周期

   (5)Fragment与Activity 之间相互通信(传值)


3. Activity -> Fragment 传参

   3.1 基本思路

      Activity -> Fragment 创建 Bundle ,Fragment 中 setArgsuments(Bundle bundle)

   3.2 实现

  (1)新建MyFragment4 类

             实现 Fragment 类,实现 onCreateView 方法

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// layout布局文件 转成view 对象/** * resource 布局文件 * root 加载layout 的父 ViewGroup * attachToRoot :false 不返回 */View view=inflater.inflate(R.layout.fragment, container, false);TextView textView1=(TextView) view.findViewById(R.id.textView1);Bundle bundle=getArguments();String name=bundle.getString("name");Toast.makeText(getActivity(),"MyFragment4 拿到值了",Toast.LENGTH_SHORT).show();textView1.setText(name);return view;}

 (2)在 Acitivty 中 动态加载 和传参

      

                               String str=edit.getText().toString();MyFragment4 fragment4=new MyFragment4();Bundle bundle=new Bundle();bundle.putString("name",str);fragment4.setArguments(bundle);FragmentManager Manager=getFragmentManager();FragmentTransaction transaction=Manager.beginTransaction();transaction.add(R.id.linear, fragment4);    transaction.commit();    Toast.makeText(getApplicationContext(),"Fragment3Activity已发送",Toast.LENGTH_SHORT).show();

4. Fragment -> Activity 通信

    使用 Interface 实现回调函数 ,进行传参

    4.1 实现

    (1)在Fragment4 中 中定义回调接口

         

package com.example.fragment;import com.example.studydemo.R;import android.annotation.SuppressLint;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;import android.widget.Toast;@SuppressLint("NewApi")public class MyFragment4 extends Fragment {//初始化private FragmentToActivity fragmentToActivity;public interface FragmentToActivity{public void huidiao(String str);}@Overridepublic void onAttach(Activity activity) {// 实例话 接口 对象 ,调用传值fragmentToActivity=(FragmentToActivity) activity;super.onAttach(activity);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// layout布局文件 转成view 对象/** * resource 布局文件 * root 加载layout 的父 ViewGroup * attachToRoot :false 不返回 */View view=inflater.inflate(R.layout.fragment, container, false);TextView textView1=(TextView) view.findViewById(R.id.textView1);Bundle bundle=getArguments();String name=bundle.getString("name");Toast.makeText(getActivity(),"MyFragment4 拿到值了",Toast.LENGTH_SHORT).show();textView1.setText(name);//调用接口传参fragmentToActivity.huidiao("我是MyFragment4 , 已经拿到值了,哈哈!");return view;}}

   4.2 Activity 实现接口 

       处理参数,即为回调的值

@Overridepublic void huidiao(String str) {// 回调函数 取值Toast.makeText(getApplicationContext(),"我是 Fragment3 回调函数执行了",Toast.LENGTH_SHORT).show();tv_frag3.setText(str);}

5.总结

  Fragment 的基本知识就完了,后面在使用ViewPager的时候,需要配合Fragment来使用;


6.demo 下载

http://download.csdn.net/detail/lablenet/9041457



0 0
原创粉丝点击