MainActivity如下:

package cc.testsimplefragment0;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentTransaction;
import cc.testsimplefragment0.TitlesListFragment.TitlesListFragmentCallBack;
/**
* Demo描述:
* Fragment基本使用以及Fragment与Activity之间数据的传递
*
* 参考资料:
* Android疯狂讲义(第二版)
*
* 备注说明:
* 利用接口实现Fragment与Activity的信息传递.
* 这个思路是不错的.
* 在此总结一下Fragment与Activity之间交换数据的方式:
* 1 Activity向Fragment传递数据
* fragment.setArguments(bundle)
* 2 Fragment向Activity传递数据
* 在Fragment内部定义一个回调接口.让包含该Fragment的
* Activity实现该接口.这样Fragment就可调用该回调方法
* 将数据传给Activity
*
*/
public class MainActivity extends Activity implements TitlesListFragmentCallBack{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); } //实现TitlesListFragmentCallBack接口中的方法
@Override
public void onItemSelected(int index) {
DetailFragment detailFragment=new DetailFragment();
Bundle bundle=new Bundle();
bundle.putInt(Data.ID, index);
//向detailFragment传入参数
detailFragment.setArguments(bundle); //开始Fragment的事务Transaction
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
//替换容器(container)原来的Fragment
fragmentTransaction.replace(R.id.relativeLayoutContainer, detailFragment);
//设置转换效果
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//将事务添加到Back栈.即按下Back键时回到替换Fragment之前的状态.类似于Activity的返回
fragmentTransaction.addToBackStack(null);
//提交事务
fragmentTransaction.commit();
} }

TitlesListFragment如下:

package cc.testsimplefragment0;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* 备注说明:
* 因为是继承自ListFragment
* 所以不用覆写onCreateView()方法
*
*/
public class TitlesListFragment extends ListFragment {
private TitlesListFragmentCallBack mTitlesListFragmentCallBack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置适配器
ArrayAdapter<String> arrayAdapter=
new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_checked, Data.TITLES);
setListAdapter(arrayAdapter); } //当该Fragment被添加,显示到Activity时调用该方法
//在此判断显示到的Activity是否已经实现了接口
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof TitlesListFragmentCallBack)) {
throw new IllegalStateException("TitlesListFragment所在的Activity必须实现TitlesListFragmentCallBack接口");
}
mTitlesListFragmentCallBack=(TitlesListFragmentCallBack) activity;
} //当该Fragment从它所属的Activity中被删除时调用该方法
@Override
public void onDetach() {
super.onDetach();
mTitlesListFragmentCallBack=null;
} //点击ListFragment中某个条目时调用该方法
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mTitlesListFragmentCallBack.onItemSelected(position);
//设置ListView为单选模式
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//显示选中的条目
getListView().setItemChecked(position, true);
} //定义一个业务接口
//该Fragment所在Activity需要实现该接口
//该Fragment将通过此接口与它所在的Activity交互
public interface TitlesListFragmentCallBack{
public void onItemSelected(int index);
} }

DetailFragment如下:

package cc.testsimplefragment0;

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 DetailFragment extends Fragment {
private int id=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(Data.ID)) {
id=getArguments().getInt(Data.ID);
}
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.detail_fragment, container, false);
TextView titleTextView=(TextView) view.findViewById(R.id.titleTextView);
titleTextView.setText(Data.TITLES[id]); TextView detailTextView=(TextView) view.findViewById(R.id.detailTextView);
detailTextView.setText(Data.DETAILS[id]); return view;
} }

Data如下:

package cc.testsimplefragment0;
public final class Data {
public static final String ID="id";
//标题
public static final String[] TITLES = { "标题1","标题2", "标题3","标题4"};
//内容
public static final String[] DETAILS = { "内容1","内容2", "内容3","内容4"};
}

Fragment与Activity传递数据的更多相关文章

  1. 核心基础以及Fragment与Activity传递数据完整示例

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  2. android78 Fragment和Activity 传递数据

    Activity: package com.itheima.senddata; import android.os.Bundle; import android.app.Activity; impor ...

  3. Fragment+Activity传递数据

    自己经常使用的知识点,每次到要用的时候都还要再查一次才能懂得使用,终于体会到总结的必要性了. Activity传递数据给Fragment Bundle bundle_fragment=new Bund ...

  4. Activity与Fragment数据传递之Fragment从Activity获取数据

    整理Fragment与Activity之间的数据交换,大体上包括三种: 1.Fragment从Activity获取数据 2.Activity从Fragment获取数据 3.Fragment之间获取数据 ...

  5. activity与fragment之间的传递数据

    首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...

  6. Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)

    Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...

  7. Android开发:向下一个activity传递数据,返回数据给上一个activity

    1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...

  8. 两个Fragment之间如何传递数据

    FragmentA启动FragmentB,做一些选择操作后,返回FragmentA,需要把FragmentB里面选择的数据传回来.有什么办法? Fragment之间不能直接通信,必须通过Activit ...

  9. Android开发学习之路-回调实现Service向activity传递数据

    开启服务的时候,如果我们是通过bindService来绑定服务并且要向服务传递数据,可以直接在Intent中设置bundle来达到效果,但是如果是我们需要从服务中返回一些数据到Activity中的时候 ...

随机推荐

  1. 【转载】Python: Enum枚举的实现

    转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html   从C系语言过来用Python,好不容易适应了写代码不打 ...

  2. JAVA用POI读取和创建2003和2007版本Excel

    1.添加maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-o ...

  3. 四十四 常用内建模块 struct

    准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...

  4. CentOS 7 上搭建 ownCloud 私有云

    所需软件 & 环境 操作系统:CentOS 7.3.1711 最小安装 (已关闭 SELinux 和防火墙) 应用软件: Nginx  .MariaDB  .PHP  .ownCloud 10 ...

  5. JavaScript 闭包(随笔)

    闭包,伟大的闭包.... 先看看百科对百度的定义是什么样的. 百科说:闭包是指可以包含自由(未绑定到特定对象)变量的代码块:这些变量不是在这个代码块内或者任何全局上下文中定义的,而是在定义代码块的环境 ...

  6. UVa 221 Urban Elevations 城市正视图 离散化初步 无限化有限

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 221 Urban Elevations 给出城市中建筑物的x, ...

  7. org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 32; 元素类型为 "key" 的内容必须匹配 "(column)*"

    报错:部分错误信息,主要查看CauseBy Caused by: org.hibernate.InvalidMappingException: Unable to read XML at org.hi ...

  8. shell 执行结果赋给变量

    #将pwd的执行结果放到变量value中保存, value=$(pwd) 另一种方法: value=`pwd`

  9. 93.数字三角形W(深搜)

    2189 数字三角形W  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 数字三角形 要求走到最后mod 10 ...

  10. LOG收集系统(一):原日志至收集

    Date: 20140207Auth: Jin 设置一个LOG收集系统1. 收集原生(不解析,不压缩)的业务日志和WEB日志(NGINX,PHP)2. 提供给开发,测试直接阅读和下载 需求分析原生日志 ...