核心基础以及Fragment与Activity传递数据完整示例
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传递数据完整示例的更多相关文章
- Fragment与Activity传递数据
MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...
- android78 Fragment和Activity 传递数据
Activity: package com.itheima.senddata; import android.os.Bundle; import android.app.Activity; impor ...
- Fragment+Activity传递数据
自己经常使用的知识点,每次到要用的时候都还要再查一次才能懂得使用,终于体会到总结的必要性了. Activity传递数据给Fragment Bundle bundle_fragment=new Bund ...
- Activity与Fragment数据传递之Fragment从Activity获取数据
整理Fragment与Activity之间的数据交换,大体上包括三种: 1.Fragment从Activity获取数据 2.Activity从Fragment获取数据 3.Fragment之间获取数据 ...
- activity与fragment之间的传递数据
首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...
- Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)
Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...
- Android开发:向下一个activity传递数据,返回数据给上一个activity
1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...
- 两个Fragment之间如何传递数据
FragmentA启动FragmentB,做一些选择操作后,返回FragmentA,需要把FragmentB里面选择的数据传回来.有什么办法? Fragment之间不能直接通信,必须通过Activit ...
- Android开发学习之路-回调实现Service向activity传递数据
开启服务的时候,如果我们是通过bindService来绑定服务并且要向服务传递数据,可以直接在Intent中设置bundle来达到效果,但是如果是我们需要从服务中返回一些数据到Activity中的时候 ...
随机推荐
- struts2摘记
阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器FilterDi ...
- Logstash 默认不处理一天前的文件
The default behavior of the file input plugin is to ignore files whose last modification is greater ...
- Xlint以及Java Lint 选项
Java Lint 选项 Java 编译器的选项包括所谓的标准选项和非标准选项.标准选项是指在当前版本的开发环境中支持,且在未来版本中也将被支持的选项.常用的标准选项比如 -classpath 以及 ...
- [Daily] 2014-4-22
KEEP GOING Think more product when face difference Check value null when insert/remove/update/add ch ...
- 还是log4net的使用
最近做个项目要用到日志系统,这这可把我给难住了,后来问了下度娘,发现只有你想不到的,没有那些找不到的开源组件,后来发现了log4net,但是我是控制台程序,没有个实例还真不好搞,想想还是看看他的运行过 ...
- Ant 简易教程
转载:http://www.cnblogs.com/jingmoxukong/p/4433945.html Ant 简易教程 Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动 ...
- JS 把 Wed Jul 15 2015 00:00:00 GMT+0800 转换成2015-07-15
function strlen(str) { var len = 0; for (var i = 0; i < str.length; i++) { var c = str.charCodeAt ...
- SQL初级阶段笔记
DataBase Management Stystem(数据库管理系统)简称:DBSM:虽然DBSM并不等于数据库,但行业内通常将DBSM称为数据库,所以一般来说数据库就指的是DBSM. 简单来讲DB ...
- Csharp实现快速排序
public void QuickSort(int[] arr, int left, int right) //快速排序 { //先从数列中去处一个数作为基准数 //分区过程,将比这个数大的数全放在他 ...
- Lua的元方法__newindex元方法
上一篇介绍了__index元方法,总结来说:__index元方法是用于处理访问table中不存在的字段时的情况. 而今天,介绍的__newindex元方法,总结来说,就是:用于处理给table中不存在 ...