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"};
}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle"> <!-- 添加一个Fragment -->
<fragment
android:id="@+id/titlesListFragment"
android:name="cc.testsimplefragment0.TitlesListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <!-- 添加一个RelativeLayout容器 -->
<RelativeLayout
android:id="@+id/relativeLayoutContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" /> </LinearLayout>

detail_fragment.xml如下:

<?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/titleTextView"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" /> <!-- 显示详细 -->
<TextView
android:id="@+id/detailTextView"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" /> </LinearLayout>

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cc.testsimplefragment0"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cc.testsimplefragment0.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

核心基础以及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. Linux学习笔记2:如何快速的学习使用一个命令

    Linux 分层 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...

  2. Struts2 单文件上传

    Struts2 提供了更为简便的文件上传机制,将文件上传的复杂操作都封装到commons-fileupload.jar .commons-io.jar两个jar包中,然后再程序中使用简单的几句代码就能 ...

  3. Examining the Rooms(dp,斯特灵数)

    Examining the Rooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. win10 ie11 以管理员身份运行才正常

    和谐版 win10优化后 ie11不能下载 显示不正常, 以管理员身份运行才正常 ,网上攻略 ( “打开并修改注册表使用快捷键[WIN+R]打开命令行窗口,输入regedit打开注册表,在注册表中找H ...

  5. JS 更改表单的提交时间和Input file的样式

    JS转换时间 function renderTime(data) { var da = eval('new ' + data.replace('/', '', 'g').replace('/', '' ...

  6. 20个命令行工具监控 Linux 系统性能

    对于每个系统管理员或网络管理员来说,每天要监控和调试 Linux 系统性能问题都是非常困难的工作.我已经有5年 Linux 管理员的工作经历,知道如何监控系统使其保持正常运行.为此,我们编写了对于 L ...

  7. js基础——属性操作

    html属性:属性名——属性值 操作:读 . 写 读操作:用来获取.找到属性名对应的属性值,方法:元素.属性名 例如:var oBtn = document.getElementById('btn1' ...

  8. MyEclipse的 lib和Build path(构建路径)(转)

    首先两种方式对于放置jar包的方式是不同的: Build path(构建路径):对于种方式来说,可以算是对jar包文件的一个引用.可以引用lib下的jar包,也可以引用本地磁盘上的jar包. WEB- ...

  9. Linux学习之sed命令详解

    概述 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区 ...

  10. JVM学习之Eclipse输出GC日志

    Java应用启动时,可以通过设置verbose参数来输出JVM的gc情况,命令如下:-verbose:gc或者-XX:+PrintGC在Eclipse中可以通过Run As|Run Configura ...