有Xamarin Mvvmcross Android共享元素导航示例吗? [英] Is there a Xamarin Mvvmcross Android Shared Element Navigation example?

查看:65
本文介绍了有Xamarin Mvvmcross Android共享元素导航示例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使动画/过渡正常工作在我的Xamarin Android应用程序中使用Mvx.

I'm trying to get this animation/transition working in my Xamarin Android application with Mvx.

我有一张卡片的回收站视图.现在,点击卡片时,我会呼叫:

I have a recyclerview with cards. When tapping on a card, I now call:

private void TimeLineAdapterOnItemClick(object sender, int position)
{
    TimeLineAdapter ta = (TimeLineAdapter) sender;
    var item = ta.Items[position];

    int photoNum = position + 1;
    Toast.MakeText(Activity, "This is photo number " + photoNum, ToastLength.Short).Show();

    ViewModel.ShowDetails(item.Id);
}

我正试图找出如何通过Mvvmcross将此Java导航转换为Xamarin的方式:

I'm trying to find out how to translate this java navigation with transition to Xamarin with Mvvmcross:

ActivityOptionsCompat options = 
   ActivityOptionsCompat.MakeSceneTransitionAnimation(this, imageView, getString(R.string.activity_image_trans));

startActivity(intent, options.toBundle());

我知道您可以在Mvx中使用自定义演示者,但是如何获得RecyclerView中被窃听的卡片的ImageView,我想将其转换"为新的ImageView.新活动?

I know that within Mvx you can make use of custom presenters, but how do I get hold of, for example, the ImageView of the tapped Card within the RecyclerView which I would like to 'transform' to the new ImageView on the new Activity?

谢谢!

.

推荐答案

是否有Xamarin Mvvmcross Android共享元素导航 例子吗?

Is there a Xamarin Mvvmcross Android Shared Element Navigation example?

我不相信.

我知道您可以在Mvx中使用自定义演示者,但是如何 我是否掌握了例如被窃听卡的ImageView 我想转换"到新的RecyclerView 在新的Activity上使用ImageView?

I know that within Mvx you can make use of custom presenters, but how do I get hold of, for example, the ImageView of the tapped Card within the RecyclerView which I would like to 'transform' to the new ImageView on the new Activity?

我想实现共享要转换的控制元素的最简单方法是使用

The easiest way that I can think of to achieve the sharing of control elements you want to transition is via the use of view tags and a presentation bundle when using ShowViewModel.

我建议对您的适配器单击处理程序进行一些更改,以包含被选择的ViewHolder的视图(请参见

I would suggest making some changes to your Adapter Click handler to include the view of the ViewHolder being selected (See GitHub repo for example with EventArgs). That way you can interact with the ImageView and set a tag that can be used later to identity it.

private void TimeLineAdapterOnItemClick(object sender, View e)
{
    var imageView = e.FindViewById<ImageView>(Resource.Id.imageView);
    imageView.Tag = "anim_image";

    ViewModel.ShowDetails(imageView.Tag.ToString());
}

然后在您的ViewModel中,通过presentationBundle发送该标签.

Then in your ViewModel, send that tag via a presentationBundle.

public void ShowDetails(string animationTag)
{
    var presentationBundle = new MvxBundle(new Dictionary<string, string>
    {
        ["Animate_Tag"] = animationTag
    });

    ShowViewModel<DetailsViewModel>(presentationBundle: presentationBundle);
}

然后创建一个自定义演示者来拾取presentationBundle并通过过渡处理新活动的创建.定制演示者,该演示者使用标记查找您要转换的元素,并在新活动的开始中包含ActivityOptionsCompat.此示例使用的是MvxFragmentsPresenter,但是如果您不使用片段并使用MvxAndroidViewPresenter,则解决方案几乎是相同的(替代Override Show,不需要构造函数).

Then create a custom presenter to pickup the presentationBundle and handle the creating of new activity with the transition. The custom presenter which makes use of the tag to find the element that you want to transition and include the ActivityOptionsCompat in the starting of the new activity. This example is using a MvxFragmentsPresenter but if you are not making use of fragments and using MvxAndroidViewPresenter the solution would be almost identical (Override Show instead and no constructor required).

public class SharedElementFragmentsPresenter : MvxFragmentsPresenter
{
    public SharedElementFragmentsPresenter(IEnumerable<Assembly> AndroidViewAssemblies)
        : base(AndroidViewAssemblies)
    {
    }

    protected override void ShowActivity(MvxViewModelRequest request, MvxViewModelRequest fragmentRequest = null)
    {
        if (InterceptPresenter(request))
            return;

        Show(request, fragmentRequest);
    }

    private bool InterceptPresenter(MvxViewModelRequest request)
    {
        if ((request.PresentationValues?.ContainsKey("Animate_Tag") ?? false)
            && request.PresentationValues.TryGetValue("Animate_Tag", out var controlTag))
        {
            var intent = CreateIntentForRequest(request);

            var control = Activity.FindViewById(Android.Resource.Id.Content).FindViewWithTag(controlTag);
            control.Tag = null;

            var transitionName = control.GetTransitionNameSupport();
            if (string.IsNullOrEmpty(transitionName))
            {
                Mvx.Warning($"A {nameof(transitionName)} is required in order to animate a control.");
                return false;
            }

            var activityOptions = ActivityOptionsCompat.MakeSceneTransitionAnimation(Activity, control, transitionName);

            Activity.StartActivity(intent, activityOptions.ToBundle());
            return true;
        }

        return false;
    }
}

GetTransitionNameSupport是一种扩展方法,在获取TransitionName时仅进行平台API检查.

GetTransitionNameSupport is an extension method that just does a platform API check when getting the TransitionName.

public static string GetTransitionNameSupport(this ImageView imageView)
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        return imageView.TransitionName;

    return string.Empty;
}

最后一步是在您的Setup.cs

protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
    var mvxPresenter = new SharedElementFragmentsPresenter(AndroidViewAssemblies);
    Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxPresenter);
    return mvxPresenter;
}


您可以查看在GitHub上的仓库,该示例演示了此示例.该解决方案的设计使演示者不必关心正在转换的控件的类型.控件仅需要用于标识它的标签.存储库中的示例还允许指定要转换的多个控制元素(我不想在上面的示例中包含更多的复杂性).


You can check the repo on GitHub which demonstrates this example. The solution is designed so that the presenter does not have to care about the type of the control that is being transitioned. A control only requires a tag used to identify it. The example in the repo also allows for specifying multiple control elements that you want to transition (I did not want to include more complexity in the example above).

这篇关于有Xamarin Mvvmcross Android共享元素导航示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆