Fragment切換動畫,fragment切換

來源:互聯網
上載者:User

Fragment切換動畫,fragment切換

題外話:

如何把一個 “小view” 或者 “小fragment” 覆蓋到一個 “全屏view” 之上(見)。

方法一(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>

方法二(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>

 

開始正題,首先說一下為什麼用Fragment而不用ViewGroup,我覺得Fragment是一個小的執行單元,它既有Actvity處理事件的能力又有ViewGroup inflate 局部顯示的功能,避免把一堆業務上不相關的控制項和事件處理都寫在一個Actvity裡面,ViewGroup不包含任何事件處理也方便其他地方共用。

先說一下需求,由介面A 過渡到介面B,上面藍色的地區不動,FragmentA 自上而下逐漸消失,FragmentB自下而上逐漸顯示,(就是做了一個translationY動畫)。

百度Google了“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-8"?><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"        android:propertyName="translationY"        android:valueFrom="0dp"        android:valueTo="500dp"        android:valueType="floatType" /></set>

 

但是發現了一個問題,FramentA 開始做collapse_from_top動畫的時候,竟然不是以A點作為(0,0)原點開始做動畫的,而是以B點作為(0,0)作為原點,這就會導致FragmentA先跳到B點,然後再做下移動畫。

這算不算google的bug呢?

解決方案其實也挺簡單,就是把Fragment當作ViewGroup當作來用。

<fragment        android:id="@+id/a"       android:name="FragmentA"       android:layout_alignParentBottom="true"       android:layout_width="match_parent"       android:layout_height="wrap_content"/><fragment        android:id="@+id/b"       android:name="FragmentB"       android:layout_alignParentBottom="true"       android:visibility="gone"       android:layout_width="match_parent"       android:layout_height="wrap_content"/>

 

FragmentB 預設是View.gone. 

 

View a = getActivity().findViewById(R.id.a);View b = getActivity().findViewById(R.id.b);switchView(a,b,true); //a做下移動畫 b做上移動動畫。

 

注意,findViewById的參數是 fragment 的 id,返回值是Fragment的 inflate 的ViewGroup,這樣我們就可以像操作view一樣來給fragment做切換動畫了。


也可以通過fragment.getView()擷取根view然後做動畫。


擷取 xml  中的 fragment

Fragment ss = (Fragment) getActivity().getFragmentManager().findFragmentById(R.id.network_diagnostics_scanning_zone);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.