10人收藏此文章, 我要收藏发表于8个月前(2013-01-02 21:16) , 已有
1626次阅读 ,共
1个评论
写的只是个人的理解,希望有错大神们能指出来。
通过Fragments来构建你的动态Activity布局:
预先构建出模型,like this:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<FrameLayout
android:id=”@+id/ui_container”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_weight=”1”
/>
<FrameLayout
android:id=”@+id/details_container”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_weight=”3”
/>
</LinearLayout>
这是某个activity的布局,一个大容器里面包含了2个小容器,那么小容器什么用处呢?
为了动态在程序中添加fragment,预先先构建模型,到时候动态填充。(之前我们曾提到容器,其实本质就是Layout)。这样一个坑位就对应一个fragment,这也就是为什么我们FragmentManager它的方法中像add、replace都会涉及到一个容器id,其实是一个思想,先构建蓝图,然后不同模块也就是不同容器放不同的fragment,然后每个容器又可以随时更换自己的fragment,这样就实现了动态机制。
Fragments 和 Back Stack (后台的栈)
我们知道操作事务可以动态改变fragments,从而改变整个UI,那么既然涉及到了事务,从SQL学习我们知道事务一般都有回滚的机制,那么fragment的事务也有吗?
答案是肯定的,情形比如你在用户交互中通过事务改变了界面,然后用户又希望回退回上个界面,那么只要在commit之前调用addToBackStack就可以将这个事务保存起来,然后按回退按钮就可以回退了。 (~。~)
例子:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ui_container, new MyListFragment());
Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);
String tag = null;
ragmentTransaction.addToBackStack(tag); //commit之前哦!
fragmentTransaction.commit();
这样在你回退的时候,MyListFragment会被移除,DetailFragment会被重启恢复。
注意:非常注意:如果你的fragment是在xml中直接定义的,你是无法正常使用一些事务的(比如回退什么的),所以用事务你的fragment必须用事物增加进来的。
按照我的话来说,从哪里来就从哪里回去。
如何使用Fragment的过度动画,又如何自定义自己的动画?
要使用默认的动画,需要使用FragmentTransaction的方法setTransition方法,传递FragmentTransaction.TRANSIT_FRAGMENT_*,就像FragmentTransaction.TRANSIT_FRAGMENT_OPEN.
例子:transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
你也可以自定义自己的过度动画,使用setCustomAnimations()方法,里面传递2个XML动画资源,第一个参数的动画是在事务加入fragment的时候,第二个参数的动画是在事务移除或者替换了某个fragment时触发。
例子:fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
注意:animator是3.0引入的(API Level 11)动画,3.0之后的应该使用animator(Property Animation)这个,3.0之前support library 的只能用以前的动画资源。
Fragments 和 Activities之间的接口:
在Fragment类中,你可以使用getActivity便可获得与其绑定的activity。这对找到当前的上下文对象(context)非常有用,可以访问其他的fragments通过FragmentManager,还可以找到层次布局中的某个View.
TextView textView = (TextView)getActivity().findViewById(R.id.textview);
可能有种需求,如何让你的fragment与activity共享事件呢?看例子
//这是某个fragment类的片段: 通过定义接口,让后让activity去实现这个接口,fragment这边在onAttach的时候获取到activity的引用,然后通过多态可以将其转化为接口对象,然后你就可以做共享事件的事儿了。
01 | public interface OnSeasonSelectedListener { |
02 | public void onSeasonSelected(Season season); |
05 | private OnSeasonSelectedListener onSeasonSelectedListener; |
06 | private Season currentSeason; |
09 | public void onAttach(Activity activity) { |
10 | super .onAttach(activity); |
13 | onSeasonSelectedListener = (OnSeasonSelectedListener)activity; |
14 | } catch (ClassCastException e) { |
15 | throw new ClassCastException(activity.toString() + |
16 | “ must implement OnSeasonSelectedListener”); |
20 | private void setSeason(Season season) { |
21 | currentSeason = season; |
22 | onSeasonSelectedListener.onSeasonSelected(season); |
没有布局的Fragments的使用:
在绝大多数情况,fragments还是用来分装模块化的UI设计而生的,然而你也可以创建一个没有UI的fragment,用来提供后台的行为(比如保存activity的状态。(当配置发生改变造成activity重启)).
下面给出些代码片段:
01 | public class NewItemFragment extends Fragment { |
03 | public void onAttach(Activity activity) { |
04 | super .onAttach(activity); |
10 | public void onCreate(Bundle savedInstanceState) { |
11 | super .onCreate(savedInstanceState); |
17 | public void onActivityCreated(Bundle savedInstanceState) { |
18 | super .onActivityCreated(savedInstanceState); |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(workerFragment, MY_FRAGMENT_TAG); //因为没有UI所以不能指定父容器ID,只能指定tag了。
fragmentTransaction.commit();
其它的Fragments: (这些暂时不讨论,是不是很像activity的扩展呢?)
1.DialogFragment
2.ListFragment
3.WebViewFragment