Android fragment animation output not firing
I have a snippet that appears when added and slides off the screen to the right when removed. However, only the sliding animation works, and when I set the fragment's visibility it disappears without sliding. This is also my first post, so please let me know if I am not doing something right, thanks!
Operation code
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in, R.animator.slide_out_right);
Bundle bundle = new Bundle();
MyFragment myFrag = new MyFragment();
myFrag.setArguments(bundle);
ft.add(R.id.challenger_preview_fragment_container, myFrag, "MyFragment");
ft.commit();
}
Snippet code
public void removeFragment() {
getView().setVisibility(View.GONE);
}
XML animation
slide_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="1000"
android:valueTo="0"
android:valueType="floatType" />
</set>
slide_out_right
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:valueFrom="-1280"
android:valueTo="0"
android:valueType="floatType"
android:propertyName="X"
android:duration="2000" />
</set>
+3
source to share
2 answers
Try the below code, below create animation object to bring it to the right and set duration. Create a listener later to know when the animation ends so you can hide your view.
The problem with your previous code is that you hide the view until the animation ends.
Animation animation = AnimationUtils.loadAnimation(getActivity(),R.animator.slide_out_right);
animation.setDuration(800);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
try {
getView().setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
//Start the animation.
getView().startAnimation(animation);
+2
source to share