Android Fragment之间传值
首先来介绍的是我们的默认的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <fragment
android:id="@+id/fragment1"
android:name="com.example.chuang.frgone"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.chuang.frgtwo"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" /> </LinearLayout>
需要注意的属性也就fragment中的那个name属性,这个属性很关键,作用就是在这个布局被加载时,fragment自动实例化对应的类

既然要加载两个类文件,所以就必须加载对应的布局,

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" > <ListView
android:id="@+id/ls_show"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </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" > <TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>
然后我们需要做的就是将布局文件和类进行绑定
package com.example.chuang; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class frgtwo extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_two, container);
}
}
其他的绑定也是类似的,运行的效果如下图,在这里为你看清楚,我在Fragment_one中添加了背景颜色,

为了方便测试,我们定义了如下的字段
package com.example.chuang; import java.util.ArrayList; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class frgone extends Fragment {
private ListView ls=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_one, container);
ls=(ListView)v.findViewById(R.id.ls_show);
ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray()));
return v; }
private ArrayList<String> GetArray()
{
ArrayList<String> al=new ArrayList<String>();
al.add("我就是我");
al.add("点亮我的新");
al.add("你的心,我的鑫");
return al;
}
}
效果图如下

那现在我们要做的呢就是在左边点击,右边显示,也就是实现Fragment之间的传值
第一种方法,也就是利用回调
package com.example.chuang; import java.util.ArrayList; 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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class frgone extends Fragment {
private CallBack back=null;
private ListView ls=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_one, container);
ls=(ListView)v.findViewById(R.id.ls_show);
ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray()));
ls.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(back!=null)
{
back.SetTeee(String.valueOf( parent.getItemAtPosition(position)));
//back.SetTeee(getString(getId()));
}
} });
return v;
}
private ArrayList<String> GetArray()
{
ArrayList<String> al=new ArrayList<String>();
al.add("我就是我");
al.add("点亮我的新");
al.add("你的心,我的鑫");
return al;
}
public interface CallBack
{
void SetTeee(String s);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(activity instanceof CallBack)
{
back=(CallBack)activity;
}
}
}
然后是主Activity的代码
@Override
public void SetTeee(String s) {
View v= this.findViewById(R.id.fragment2);
TextView t=(TextView)v.findViewById(R.id.tv_show);
t.setText(s);
}
效果图如下,这样我们就实现了第一种方法

第二种方法
使用第二种方法,我们首先需要来了解一下Fragment 和Activity的生命周期

我们可以根据上面的那个图,可以知道,只有当fragment创建完毕,活动就会通知他们调用onActivityCreated()方法,来告知
所以我们就可以在这里获取到主活动里面的一切,不是想干嘛就干嘛吗0-0!
贴上代码
package com.example.chuang; import java.util.ArrayList; 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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; public class frgone extends Fragment {
private CallBack back=null;
private ListView ls=null;
private TextView tv=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_one, container);
ls=(ListView)v.findViewById(R.id.ls_show);
ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray()));
ls.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(back!=null)
{
if(tv!=null)
tv.setText(String.valueOf( parent.getItemAtPosition(position)));
}
} });
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View v= getActivity().findViewById(R.id.fragment2);
tv=((TextView)v.findViewById(R.id.tv_show)); }
private ArrayList<String> GetArray()
{
ArrayList<String> al=new ArrayList<String>();
al.add("我就是我");
al.add("点亮我的新");
al.add("你的心,我的鑫");
return al;
}
第三种方法,那就是在Activity中来管理两个Fragment,当然做法也是非常的简单,只需要在Activity的start生命周期后就可以
Android Fragment之间传值的更多相关文章
- [转][译][Android基础]Android Fragment之间的通信
2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...
- Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)
1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- Android Fragment之间传递List数据
要说的是在两个Fragment之间传递List数据,比如有个List<User>,以及传递字符串数据,比如testId,该如何从FragmentA传递到FragmentB呢? 下面这个例子 ...
- Android - fragment之间数据传递
<Fragment跳转时传递参数及结果回传的方法> <Fragment详解之五——Fragment间参数传递> <Android解惑 - 为什么要用Fragment.se ...
- Android fragment之间消息传递
1. 在Fragment里定义一个内部接口, 在Fragment初始化方法里, 把父Activity转换成这个接口, 赋值给成员变量 public class DummyFragment extend ...
- Fragment之间传值
Activity: String myArguments; public String getarguments() { return myArguments; } public void ...
- Android activity之间传值关键性代码
从当前activity中获取et 表单中的值,并跳转到myactivity.java所绑定的xml布局文件上. private EditText et; Intent intent=new Inten ...
- android fragment传递参数_fragment之间传值的两种方法
在Activity中加载Fragment的时候.有时候要使用多个Fragment切换.并传值到另外一个Fragment.也就是说两个Fragment之间进行参数的传递.查了很多资料.找到两种方法.一种 ...
- Android——软键盘操作+fragment之间传递参数+TextView限制字数,显示"..."
原文地址: Android 手动显示和隐藏软键盘 Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果) Android Fragment传递参数_Fragm ...
随机推荐
- linux 查看各服务状态chkconfig
使用chkconfig 查看服务状态启动状态chkconfig --list 查看服务状态chkconfig --del <service name> 删除掉某项服务.在Fedora14中 ...
- location.hash 详解
前年9月twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改版后,就变成了 h ...
- PHP正则匹配title标题文本
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- DHTMLX 前端框架 建立你的一个应用程序 教程(五)--添加一个表格Grid
表格例子 样本如下: 我们这篇介绍的是dhtmlxGrid 组件. 它支持4种数据格式:XML, JSON, CSV, JSArray. 添加表格到布局的单元格中去: 1.使用attachGrid( ...
- *[topcoder]LittleElephantAndIntervalsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...
- 164. Maximum Gap
题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...
- RPGJS 进阶分析之 如何使用RMXP导出的数据
首先启动RMXP 在主脚本编辑区粘贴官方提供的导出数据脚本. 之后启动并进入游戏之后按F8键就可以调出导出Map和Animation的菜单 导出后的数据保存在工程目录下的RpgJs目录下. Datab ...
- Java-Swing嵌入浏览器(一)
今天要说的额是浏览器的第一个版本是用DJnative-swt和swt包开发的调用本地浏览器和webkit浏览器的示例 这是我的工程目录[源码见最后]: src下为写的源码,lib为引入的swt和DJn ...
- 【HDOJ】1061 Rightmost Digit
这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...
- Subline Text默认设置文件Preferences.sublime-settings—Default详解
Subline Text中,点击Preferences,选择Settings - Default 全部属性解析 // While you can edit this file, it's best t ...