I have a problem with override Custom Animation on my Fragment. I have on my Home view three buttons (First, Second and Third) and when I'm inside this views I want swipe between those view and I need swipe animation effect from left to right and from right to left etc..
For Example my SecondFragments looks like this:
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
public class SecondFragment : BaseFragment<SecondViewModel>, View.IOnTouchListener
{
private Easter _easter;
protected override int FragmentId => Resource.Layout.fragment_second;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
_easter = new Easter(new KonamiCode());
var easyEgg = new CustomEgg("Easy")
.WatchForSequence(Command.SwipeLeft(), Command.SwipeRight());
_easter = new Easter(easyEgg);=
_easter.CommandDetected += cmd => DoSwipe(cmd.Value);
var coreLayout = view.FindViewById<LinearLayout>(Resource.Id.coreLayout);
coreLayout?.SetOnTouchListener(this);
return view;
}
private void DoSwipe(string swipeText)
{
if (swipeText.Equals("LEFT"))
{
Activity.OverridePendingTransition(Resource.Animator.slide_to_right, Resource.Animator.slide_from_left);
}
if (swipeText.Equals("RIGHT"))
{
Activity.OverridePendingTransition(Resource.Animator.slide_to_left, Resource.Animator.slide_from_right);
}
ViewModel.SwipeView(swipeText);
}
public bool OnTouch(View v, MotionEvent e)
{
_easter.OnTouchEvent(e);
return true;
}
}
Method ViewModel.SwipeView looks like:
public override void SwipeView(string swipeText)
{
if (swipeText.Equals("RIGHT"))
{
Close(this);
UserDialogs.Instance.Toast("RIGHT SWIPE!");
ShowViewModel<FirstViewModel>();
}
if (swipeText.Equals("LEFT"))
{
Close(this);
UserDialogs.Instance.Toast("LEFT SWIPE!");
ShowViewModel<ThirdViewModel>();
}
}
I tried Activity.OverridePendingTransition for this but it doesnt work. I tried something with TransactionManager but still doesnt work. I need just override animations only for these three view no for whole app.
For example my test project is HERE on github.
OverridePendingTransition allows you to specify a custom animation when starting an activity from outside the
Context
of the current topActivity
. That meansOverridePendingTransition
method only be called immediately after one of the flavors ofstartActivity(Intent)
orfinish()
to specify an explicit transition animation to perform next. So when you swipe between thoseFragment
, it has no effect.An animation resource can define one of two types of animations: Property Animation and View Animation.
Property Animation
File Location:
SYNTAX:
The file must have a single root element: either
<set>
,<objectAnimator>
, or<valueAnimator>
. You can group animation elements together inside the<set>
element, including other<set>
elements.View Animation
File Location:
SYNTAX:
The file must have a single root element: either an
<alpha>
,<scale>
,<translate>
,<rotate>
, or<set>
element that holds a group (or groups) of other animation elements (even nested<set>
elements).Since your animation use
translate
, I suggest you put thesexml
file inResource/anim
folder.Since your base
Activity
isMvxCachingFragmentCompatActivityas
, you can overrideOnBeforeFragmentChanging
method to set a custom transition animation.SetCustomAnimations
set specific animation resources to run for the fragments that are entering and exiting in this transaction. The popEnter and popExit animations will be played for enter/exit operations specifically when popping the back stack.EDIT :
Custom animation for every swipe between
Fragment
, for everyFragment
, you could custom animation like this :