题外话:
如何把1个 “小view” 或 “小fragment” 覆盖到1个 “全屏view” 之上(见下图)。
方法1(FrameLayout + layout_marginTop ):
<FrameLayout
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<fragment
android:layout_marginTop= "10dp"
android:layout_width= "match_parent"
android:layout_height= "wrap_content" />
</FrameLayout>
|
方法2(RelativeLayout + layout_alignParentBottom):
<RelativeLayout
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<fragment
android:layout_alignParentBottom= "true"
android:layout_width= "match_parent"
android:layout_height= "wrap_content" />
</RelativeLayout>
|
开始正题,首先说1下为何用Fragment而不用ViewGroup,我觉得Fragment是1个小的履行单元,它既有Actvity处理事件的能力又有ViewGroup inflate 局部显示的功能,避免把1堆业务上不相干的控件和事件处理都写在1个Actvity里面,ViewGroup不包括任何事件处理也方便其他地方共用。
先说1下需求,由界面A 过渡到界面B,上面蓝色的区域不动,FragmentA 自上而下逐步消失,FragmentB自下而上逐步显示,(就是做了1个translationY动画)。
百度谷歌了“Fragment 切换动画”,发现都是setCustomAnimations来做的,这个动画要求必须是objectAnimator (属性动画)
Fragment
fragment = Fragment.instantiate(context, fragmentName, fragmentArgs);
FragmentTransaction
transaction = ((Activity) context).getFragmentManager().beginTransaction();
fragment.setArguments(fragmentArgs);
transaction.setCustomAnimations(R.anim.expand_to_top,
R.anim.collapse_from_top);
transaction.replace(R.id.network_diagnostics_status_zone,
fragment).commit();
|
<?xml
version= "1.0" encoding= "utf⑻" ?>
<set>
<objectAnimator
xmlns:android= "<a
href="http://schemas.android.com/apk/res/android" "="" style="color: rgb(50, 108, 166); text-decoration: none; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius:
0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding:
0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important;">http://schemas.android.com/apk/res/android"
android:duration= "600"
------分隔线----------------------------
------分隔线----------------------------
|