• 两个fragment传值

    方式一

    • 布局文件代码:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      tools:context=".MainActivity" > <!-- 静态 必须要写ID--> <fragment
      android:id="@+id/fragment1"
      android:name="com.qf.day14_fragment_4.fragment.MyFragment1"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" /> <View
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#0f0" />
      <!-- 动态 --> <LinearLayout
      android:id="@+id/ll_fragment_id"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:orientation="horizontal" /> </LinearLayout>
    • 布局文件逻辑代码:

      package com.qf.day14_fragment_4;
      
      import com.qf.day14_fragment_4.fragment.MyFragment2;
      
      import android.os.Bundle;
      import android.app.Activity;
      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
      import android.view.Menu; public class MainActivity extends Activity { @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); //管理者对象
      FragmentManager manager = getFragmentManager();
      FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); transaction.replace(R.id.ll_fragment_id,myFragment2); transaction.commit();
      } @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
      } }
    • 其中一个fragment逻辑代码:

      package com.qf.day14_fragment_4.fragment;
      
      import android.app.Fragment;
      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
      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 com.qf.day14_fragment_4.R; public class MyFragment1 extends Fragment{ private Button btn; @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View v = inflater.inflate(R.layout.fragment1_layout, container, false); btn = (Button) v.findViewById(R.id.btn);
      //点击按钮传值
      btn.setOnClickListener(new OnClickListener() { @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub //管理者对象
      FragmentManager manager = getFragmentManager();
      FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); Bundle bundle = new Bundle(); bundle.putString("msg", "左边Fragment向右边Fragment传值"); myFragment2.setArguments(bundle); transaction.replace(R.id.ll_fragment_id, myFragment2); transaction.commit();
      }
      });
      return v;
      }
      }

    方式二 前提:必须两个静态fragment

    • 界面xml代码:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      tools:context=".MainActivity" > <fragment
      android:id="@+id/leftfragment"
      android:name="com.qf.day14_fragment_demo5.fragment.Fragment1"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" /> <View
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#00f" /> <fragment
      android:id="@+id/rightfragment"
      android:name="com.qf.day14_fragment_demo5.fragment.Fragment2"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" /> </LinearLayout>
    • 其中核心fragment代码

      package com.qf.day14_fragment_demo5.fragment;
      
      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.EditText;
      import android.widget.TextView; import com.qf.day14_fragment_demo5.R; public class Fragment1 extends Fragment{ private EditText etContent;
      private Button btnSend; @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // TODO Auto-generated method stub View view = inflater.inflate(R.layout.layout01, container, false);
      etContent = (EditText) view.findViewById(R.id.et_content);
      btnSend = (Button) view.findViewById(R.id.btn_send); btnSend.setOnClickListener(new OnClickListener() { @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub String msg = etContent.getText().toString().trim(); //通过Fragment的id 获取Fragment2的对象
      // Fragment2 fragment2 = (Fragment2) getFragmentManager().findFragmentById(R.id.rightfragment);
      // fragment2.setTextValues(msg); //TextView tv = (TextView) getFragmentManager().findFragmentById(R.id.rightfragment).getView().findViewById(R.id.tv_show);
      TextView tv = (TextView) getActivity().findViewById(R.id.tv_show);
      tv.setText(msg); } }); return view;
      } }
  • fragment给界面传值(Activity) 接口回调

    • 界面代码

      package com.qf.day14_fragment_demo3;
      
      import com.qf.day14_fragment_demo3.callback1.CallBackValue;
      
      import android.os.Bundle;
      import android.annotation.SuppressLint;
      import android.app.Activity;
      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
      import android.view.Menu;
      import android.widget.TextView; public class MainActivity extends Activity implements CallBackValue{ private TextView tv; @SuppressLint("NewApi")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); FragmentManager manager = getFragmentManager();
      FragmentTransaction transaction = manager.beginTransaction();
      transaction.replace(R.id.ll_fragment_id, new MyFragment());
      transaction.commit();
      } //回调接口的方法
      @Override
      public void sendMessage(String msg) {
      // TODO Auto-generated method stub
      tv.setText(msg);
      } }
    • 接口代码

      package com.qf.day14_fragment_demo3.callback1;
      
      public interface CallBackValue {
      
          public void sendMessage(String msg);
      
      }
    • fragment代码

      package com.qf.day14_fragment_demo3;
      
      import com.qf.day14_fragment_demo3.callback1.CallBackValue;
      
      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.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.Button;
      import android.widget.EditText; @SuppressLint("NewApi")
      public class MyFragment extends Fragment{ CallBackValue callBackValue; private EditText et;
      private Button btnFTA; /**
      * 与Activity第一次链接是调用
      */
      @Override
      public void onAttach(Activity activity) {
      // TODO Auto-generated method stub
      super.onAttach(activity); //Fragment归属的Activity是getActivity
      callBackValue = (CallBackValue) getActivity();
      } @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View v = inflater.inflate(R.layout.fragment_layout, container,false);
      et = (EditText) v.findViewById(R.id.et);
      btnFTA = (Button) v.findViewById(R.id.btn_FTA); btnFTA.setOnClickListener(new OnClickListener() { @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub
      //将值传给Activity
      String msg = et.getText().toString().trim(); callBackValue.sendMessage(msg);
      }
      }); return v;
      } }
  • fragment 获取 界面数据(Activity)

    • 可以fragment中直接getActivity 或者getContext 获取其对象
    • 或者动态创建时用setAgunment 在fragment中getAgument

14 fragment传值的更多相关文章

  1. 14 fragment 创建

    静态展示 注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点 步骤一:创建一个类继承 Fragment 代码类型一: package com.fmy.demo1; im ...

  2. 14 Fragment 碎片总结

    Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...

  3. Android FragmentActivity 给Fragment传值

    1.Fragment给Activity传值 定义一个在fragment 中 定义interface 监听器,让activity实现,并将activity的引用传递给fragment.即setListe ...

  4. 【android】activity、fragment传值例子

    1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...

  5. Activity向Fragment传值

    发送数据 //Activity传值,通过Bundle Bundle bundle = new Bundle(); bundle.putString("MainActivity", ...

  6. 【Android】安卓开发之activity如何传值到fragment,activity与fragment传值

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家知道,我们利用activity使 ...

  7. 两个activity或者activity和fragment传值

    使用Fragment的时候可能需要在两个Fragment之间进行参数的传递,开始想着可以使用SharedPreferences进行处理,想想这些简单的参数没有必要使用这么麻烦的方式去实现,翻了一下Fr ...

  8. 14 Fragment的V4包的使用

    activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  9. 14 Fragment 注意点

    API 过时问题 API 23过时 public void onAttach(Activity activity)替换为public void onAttach(Context context) 注意 ...

随机推荐

  1. 【BZOJ4196】【Noi2015】软件包管理器

    原题传送门 题意: 给你一棵树,有2种操作: 1.使得某个点到根节点路径上的所有点权值赋为1. 2.使得某节点的子树中所有节点权值赋为0. 每次操作要求输出权值更改的节点个数. 解题思路: 显然是用树 ...

  2. 网络基础-再议TCP

    以前只是知道3次握手和4次挥手,但是对于其在连接和断开时的各个状态却不是很懂,今天就来看一下握手和挥手时的状态转换图: 1.三次握手和四次挥手时的状态转换图: 实线表示应用程序: 应用层首先发SYN的 ...

  3. 网络硬盘NFS

    NFS是网络文件系统,用于计算机间共享文件系统,由sun公司1985年推出的协议,现在已经被广泛使用.一般来说,所有的linux发型版都支持NFS.nfs是一个服务器,客户端的架构,建立一个nfs的服 ...

  4. 使用PHP脚本远程部署git项目

    准备工作: 1.coding.net创建私有项目 2.安装了Web服务 Git服务的服务器 服务器端: 1.nginx.php-fpm统一用www用户 www 目录,这个可以通过修改配置文件实现. [ ...

  5. 文件上传,服务端压缩文件方法,重点是png与gif图片的压缩,保证了透明度与动画

    /// <summary> /// 上传文件帮助类 /// </summary> public class ImageUploadHelper { #region SaveVi ...

  6. delphi 线程教学第五节:多个线程同时执行相同的任务

    第五节:多个线程同时执行相同的任务   1.锁   设,有一个房间 X ,X为全局变量,它有两个函数  X.Lock 与 X.UnLock; 有如下代码:   X.Lock;      访问资源 P; ...

  7. JAVA中的枚举类

    某些情况下一个类的对象是有限而且固定的,例如性别就只有两个类(考虑大众情况).因此这种实例有限而且固定的类,java里面叫枚举类.枚举类的关键字是enum,一些基本的命名规则和文件命名等细节和一般的类 ...

  8. jQuery 遍历 – 后代

    后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...

  9. GDAL打开mdb文件失败解决方法

    使用GDAL打开mdb文件时提示下面错误信息: ERROR 1: Unable to initialize ODBC connection to DSN for DRIVER=Microsoft Ac ...

  10. SSM实现秒杀系统案例

    ---------------------------------------------------------------------------------------------[版权申明:本 ...