2017-03-16 77 views
0

我正在Android中開發應用程序。我正在使用一個流程來獲取用戶的信息。爲了構建流,我使用了很少的片段。有五個步驟,我正在使用五個片段。我正在使用另一個片段來顯示他使用列表視圖保存的以前的記錄。在我的活動中,我使用了一個名爲Expand的按鈕。按鈕Expand用於使用片段顯示以前的記錄。當用戶點擊按鈕展開片段將發生,展開按鈕文本將被設置爲隱藏。當按鈕文本爲隱藏時,如果用戶再次點擊按鈕,片段將從堆棧中移除,並且將顯示添加到後退堆棧中的前一個片段。在Android中向上和向下滑動片段事務的動畫

例如,讓我們假設我有一個名爲FragmentA,FragmentB,FragmentC,FragmentD,FragmentE並命名ProjectRowsFragment另一個片段將被用來顯示以前保存在一個ListView名爲按鈕的點擊事件記錄5個片段擴大。

讓我們假設用戶在FragmentC中,並且他點擊了Expand按鈕。會發生什麼是FragmentC將被替換,並且ProjectRowsFragment將被添加。如果用戶再次點擊按鈕,ProjectRowsFragment將被替換,並且FragmentC將從後面堆棧中進入。如果它是FragmentD,那麼它將被替換,並且ProjectRowsFragment將被添加,並且如果用戶再次單擊該按鈕,ProjectRowsFragment將被替換,並且FragmentD將從後端堆棧中進入。

我已經完成了交易。

我想要的是我想要添加動畫,而ProjectRowsFragment(我用來顯示記錄的片段)被顯示和替換。當它顯示出來時,它會從頂部滑下,然後當它從後面的堆疊中移出時,它會向上滑動。

嘗試了很多之後,我完成了向下滑動效果,但是如何獲得向上滑動動畫。

這是我的代碼。

fragmentManager = getFragmentManager(); 
fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.setCustomAnimations(R.animator.slide_in_from_top, 0, R.animator.slide_in_from_bottom, 0); 
fragmentTransaction.replace(R.id.fragment_container, ProjectRowsFragment.newInstance(this.projectId)); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 
projectRowsExpanded = true; 

slide_in_from_top.xml文件是

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="true" > 
<objectAnimator 
    android:duration="600" 
    android:propertyName="y" 
    android:valueFrom="-1280" 
    android:valueTo="0" 
    android:valueType="floatType" /> 
</set> 

在這裏,我有三個圖像來可視化

初始步驟

enter image description here

如果按鈕上的用戶點擊指示的名單將被放置。

enter image description here

如果用戶在指定的按鈕,再次點擊。

enter image description here

+0

您的通過4個參數你的'setCustomAnimations'。你可以只嘗試2個參數'.setCustomAnimations(in,out)' –

回答

0

而是應用自定義動畫片段,您可以添加動畫到您的FrameLayout,通過傳遞您的片段容器以下列函數展開和摺疊:

FrameLayout v = (FrameLayout) findViewById(R.id.fragment_container); 
expand(v); //To Expand 
collapse(v); //To Collapse 


public static void expand(final View v) { 
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    final int targetHeight = v.getMeasuredHeight(); 

    v.getLayoutParams().height = 1; 
    v.setVisibility(View.VISIBLE); 
    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      v.getLayoutParams().height = interpolatedTime == 1 
        ? ViewGroup.LayoutParams.WRAP_CONTENT 
        : (int) (targetHeight * interpolatedTime); 
      v.requestLayout(); 
     } 

     @Override 
     public boolean willChangeBounds() { 
      return true; 
     } 
    }; 

    a.setDuration((int) (targetHeight/v.getContext().getResources().getDisplayMetrics().density)); 
    v.startAnimation(a); 
} 

public static void collapse(final View v) { 
    final int initialHeight = v.getMeasuredHeight(); 

    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      if (interpolatedTime == 1) { 
       v.setVisibility(View.GONE); 
      } else { 
       v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime); 
       v.requestLayout(); 
      } 
     } 

     @Override 
     public boolean willChangeBounds() { 
      return true; 
     } 
    }; 

    a.setDuration((int) (initialHeight/v.getContext().getResources().getDisplayMetrics().density)); 
    v.startAnimation(a); 
}