详解Android Fragment之五:Fragment与Activity的通信
2016-03-01 17:45
686 查看
Android中对于Fragment的更优雅的使用方式以及数据传递。https://github.com/peerless2012/SimpleFragment 朋友Github的项目
Android中对于Fragment的更优雅的使用方式以及数据传递。
查找要显示的Fragment
如果不存在则创建新的并添加(add)进去
如果已经存在,则隐藏当前的Fragment,显示找到的Fragment
切换的时候
以全屏的方式展示Fragment
加到回退栈中,模拟Activity的栈结构
Fragment与Activity的通信
http://www.jizhuomi.com/android/course/381.html
尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。
Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:
ViewlistView =getActivity().findViewById(R.id.list);
同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:
ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment)
Activity响应Fragment的事件
有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。
例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。如下例,OnArticleSelectedListener接口在fragmentA中定义:
Java代码
public static class FragmentA extends ListFragment{
...
//Container Activity must implement this interface
public interface OnArticleSelectedListener{
public void onArticleSelected(Uri articleUri);
}
...
然后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,如下所示:
Java代码
public static class FragmentA extends ListFragment{
OnArticleSelectedListener mListener;
...
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
mListener =(OnArticleSelectedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
}
}
...
如果activity没有实现那个接口,fragment抛出ClassCastException异常。如果成功了,mListener成员变量保存OnArticleSelectedListener的实例。于是fragmentA就可以调用撒mListener的方法来与activity共享事件。例如,如果fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentA的onListItemClick()方法,在这个方法中调用onArticleSelected()来与activity共享事件,如下:
Java代码
public static class FragmentA extends ListFragment{
OnArticleSelectedListener mListener;
...
@Override
public void onListItemClick(ListView l,View v,int position,long id){
//Append the clicked item's row ID with the content provider Uri
Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);
//Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
}
...
onListItemClick()传入的参数id是列表的被选中的行ID,另一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。
SimpleFragment
Android中对于Fragment的更优雅的使用方式以及数据传递。
以TAB的方式切换Fragment
查找要显示的Fragment如果不存在则创建新的并添加(add)进去
如果已经存在,则隐藏当前的Fragment,显示找到的Fragment
private Fragment preFragment; private void changeFragment(String fragmentName,String tag,Bundle data) { FragmentManager fragmentManager = getSupportFragmentManager(); Fragment currentFragment = fragmentManager.findFragmentByTag(tag); if (currentFragment != null && currentFragment == preFragment) return; FragmentTransaction transaction = fragmentManager.beginTransaction(); if (currentFragment == null) { currentFragment = Fragment.instantiate(this, fragmentName, data); transaction.add(R.id.fragment_content, currentFragment, tag); if (preFragment != null) { transaction.hide(preFragment); } }else { transaction.hide(preFragment); transaction.show(currentFragment); } preFragment = currentFragment; transaction.commit(); }
切换的时候
Bundle bundle3 = new Bundle(); bundle3.putString(TabFragment1.CONTENT, "这是传递给第三个TabFragment的数据"); changeFragment(TabFragment3.class.getName(),TabFragment3.TAG, bundle3);
以加入到后退栈的方式切换Fragment
以全屏的方式展示Fragment加到回退栈中,模拟Activity的栈结构
Fragment与Activity的通信
http://www.jizhuomi.com/android/course/381.html
尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。
Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:
ViewlistView =getActivity().findViewById(R.id.list);
同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:
ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment)
Activity响应Fragment的事件
有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。
例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。如下例,OnArticleSelectedListener接口在fragmentA中定义:
Java代码
public static class FragmentA extends ListFragment{
...
//Container Activity must implement this interface
public interface OnArticleSelectedListener{
public void onArticleSelected(Uri articleUri);
}
...
然后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,如下所示:
Java代码
public static class FragmentA extends ListFragment{
OnArticleSelectedListener mListener;
...
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
mListener =(OnArticleSelectedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
}
}
...
如果activity没有实现那个接口,fragment抛出ClassCastException异常。如果成功了,mListener成员变量保存OnArticleSelectedListener的实例。于是fragmentA就可以调用撒mListener的方法来与activity共享事件。例如,如果fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentA的onListItemClick()方法,在这个方法中调用onArticleSelected()来与activity共享事件,如下:
Java代码
public static class FragmentA extends ListFragment{
OnArticleSelectedListener mListener;
...
@Override
public void onListItemClick(ListView l,View v,int position,long id){
//Append the clicked item's row ID with the content provider Uri
Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);
//Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
}
...
onListItemClick()传入的参数id是列表的被选中的行ID,另一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。
相关文章推荐
- Android RecyclerView 使用完全解析
- RK29机型之Android系统启动流程
- 谈谈Android的IPC(进程间通信)机制
- 查看Android应用包名、Activity的几个方法
- OTA升级总结
- android 自定义属性中format后面引用的种类
- android framework 图解分享
- Xutils3.0下载带通知栏通知
- nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……
- android webview中调用了js的时候混淆注意事项
- 【Android】使用VideoView播放视频
- Android 自定义View实现动态炫酷按钮
- Android Studio常用快捷键
- Android的材料设计兼容库(Design Support Library)
- Android 开机自启动
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言
- Android库Volley的使用介绍
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
- Android 自定义View实现
- Android Context解析