I'm trying to animate a slide-in/slide-out
animation between Fragments
. I know there're tons of similiar question out there on StackOverflow, but believe me - I tried all of them and none is working.
Following my code (basically taken from similiar questions):
Custom Layout
public class SlidingFrameLayout extends FrameLayout {
public SlidingFrameLayout(Context context) {
super(context);
}
public SlidingFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public float getXFraction() {
final int width = getWidth();
if (width != 0) return getX() / getWidth();
else return getX();
}
public void setXFraction(float xFraction) {
final int width = getWidth();
setX((width > 0) ? (xFraction * width) : -9999);
}
}
Fragment Transaction
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.card_enter_right, R.animator.card_out_left);
ft.replace(R.id.quiz_container, CardFragment.newInstance());
ft.commit();
Object animator
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="xFraction"
android:valueFrom="1.0"
android:valueTo="0.0"
android:valueType="floatType" />
Layout file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.my.package.views.SlidingFrameLayout
android:id="@+id/quiz_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Other views -->
</FrameLayout>
So when I'm running this code nothing happens. The Fragments get replaced but without an animation.
What am doing wrong?
Note: If I'm running the animation with "x"
instead of "xFraction"
and changing the values to let's say from:1280 to:0
it's working. Unfortunately this isn't a solution for me, since I need a "percentage value" because of the broad range of display resolutions.
Solution
Thanks to pskink who led me to the solution. I always thought that the container of my Fragment needs to implement the getXFraction()/setXFraction() method. But that's not true. You're Fragments layout needs to implement getXFraction()/setXFraction
.
So basically I changed my Fragment-Layout from something like this:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:prefix="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
prefix:cardCornerRadius="4dp">
<!-- Content -->
</android.support.v7.widget.CardView>
to:
<com.my.package.views.SlidingFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:prefix="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
prefix:cardCornerRadius="4dp">
<!-- Content -->
</android.support.v7.widget.CardView>
</com.my.package.views.SlidingFrameLayout>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…