共享单元过渡没有预期的行为 [英] Shared element transition don't have the expected behaviour

本文介绍了共享单元过渡没有预期的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了共享单元从图像之间的RecyclerView到的图像中的新的片段的过渡。该RecyclerView是通过活动和目标片段主办的片段托管在其他活动的内部托管。

当我点击我的形象,转型正在发生的事情像预期,我得到我的片段。但是,当我点击后退按钮,过渡试图把我的形象在我RecyclerView第一的位置,而它的当前显示这不一定是第一个图像。

从这个情况来看,我觉得,因为它的形象,我回去的时候,因为所有的图像具有相同的过渡名字,这个过程是迷茫和刚刚获得的第一个图像设置一个过渡名称XML中,也许名单用给定的名字。所以,也许我应该动态地给予不同的过渡名字他们每个人。我还设置了相同的名称目标图像,每一个新的图像被选择时重置。但它仍然没有工作。任何人都可以帮我吗?

下面是我onItemClickListener里面的code:

  mAdapter.setOnItemClickListener(新RssItemAdapter.OnItemCLickListener(){
    @覆盖
    公共无效onItemClick(查看视图,INT位置){
        最终的RSSItem项目= mAdapter.getItem(位置);
        最后RssElementIntent意图=新RssElementIntent(getActivity(),项目位置);
        // mRootView是片段的观点
        最终ImageView的图像=(ImageView的)
mRootView.findViewById(R.id.rss_element_image);
        image.setTransitionName(的getString(R.string.transition_name_rss_feed_picture)+位置);
        ActivityOptionsCompat选项= ActivityOptionsCompat.makeSceneTransitionAnimation(
                        getActivity(),图像,RssElementFragment.EXTRA_IMAGE);
        ActivityCompat.startActivity(getActivity(),意向,options.toBundle());
    }
});

下面是目标片段的code:

  rssImage.setTransitionName(的getString(R.string.transition_name_rss_feed_picture)+ getArguments()调用getInt(ARG_ELEMENT_POSITION));
ViewCompat.setTransitionName(rssImage,EXTRA_IMAGE);

这里是的themes.xml:

 <样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>
        <! - 在这里自定义的主题。 - >
        <项目名称=colorPrimary> @彩色/ theme_primary< /项目>
        <项目名称=colorPrimaryDark> @彩色/ theme_primary_dark< /项目>
        <项目名称=机器人:colorAccent> @彩色/ theme_accent< /项目>        <! - 启用窗口内容转换 - >
        <项目名称=机器人:windowContentTransitions>真< /项目>        <项目名称=机器人:windowAllowEnterTransitionOverlap>真< /项目>
        <项目名称=机器人:windowAllowReturnTransitionOverlap>真< /项目>        &所述;! - 指定共享单元跃迁 - >
        <项目名称=机器人:windowSharedElementEnterTransition>
            @过渡/ change_image_transform
        < /项目>
        <项目名称=机器人:windowSharedElementExitTransition>
            @过渡/ change_image_transform
        < /项目>    < /风格>

编辑:

我发现我的错误,我越来越从片段rootView这是给我从我的RecyclerView,这意味着错误的转换名称随机的ImageView的ImageView的。所以我所用onItemClick给出的看法和它的工作!我也动 setTransitionName()适配器内。

  mAdapter.setOnItemClickListener(新RssItemAdapter.OnItemCLickListener(){
        @覆盖
        公共无效onItemClick(查看视图,INT位置){
            最终的RSSItem项目= mAdapter.getItem(位置);
            最后RssElementIntent意图=新RssElementIntent(getActivity(),项目位置);
            // WRONG!
            //最后ImageView的图像=(ImageView的)mRootView.findViewById(R.id.rss_element_image);
            //正确
            最终ImageView的图像=(ImageView的)view.findViewById(R.id.rss_element_image);            ActivityOptionsCompat选项= ActivityOptionsCompat.makeSceneTransitionAnimation(
                            getActivity(),图像,RssElementFragment.EXTRA_IMAGE);
            ActivityCompat.startActivity(getActivity(),意向,options.toBundle());
        }
    });


解决方案

我发现我的错误,我越来越从片段rootView这是给我从我的RecyclerView,这意味着错误的转换名称随机的ImageView的ImageView的。所以我所用onItemClick给出的看法和它的工作!我也动setTransitionName()适配器内。

  mAdapter.setOnItemClickListener(新RssItemAdapter.OnItemCLickListener(){
        @覆盖
        公共无效onItemClick(查看视图,INT位置){
            最终的RSSItem项目= mAdapter.getItem(位置);
            最后RssElementIntent意图=新RssElementIntent(getActivity(),项目位置);
            // WRONG!
            //最后ImageView的图像=(ImageView的)mRootView.findViewById(R.id.rss_element_image);
            //正确
            最终ImageView的图像=(ImageView的)view.findViewById(R.id.rss_element_image);            ActivityOptionsCompat选项= ActivityOptionsCompat.makeSceneTransitionAnimation(
                            getActivity(),图像,RssElementFragment.EXTRA_IMAGE);
            ActivityCompat.startActivity(getActivity(),意向,options.toBundle());
        }
    });

I implemented the shared element transition between from an image in a RecyclerView to an image in a new Fragment. The RecyclerView is hosted in a fragment hosted by an Activity and the target fragment is hosted inside another Activity.

When I click on my image, the transition is happening like expected and I get to my fragment. But when I click the back button, the transition is trying to put my image at the first position of my RecyclerView while it's currently displaying the image which is not necessarily the first one.

From this situation, I thought since it's a list of image in which I set a transition name inside the XML, maybe when going back, since all image has the same transition name, the process is confused and just get the first image with the given name. So maybe I should dynamically give a different transition name to each of them. I also set the same name to the target image and reset it every time a new image is selected. But it's still not working. Can anyone help me with that?

Here is the code inside my onItemClickListener:

mAdapter.setOnItemClickListener(new RssItemAdapter.OnItemCLickListener(){
    @Override
    public void onItemClick(View view, int position) {
        final RssItem item = mAdapter.getItem(position);
        final RssElementIntent intent = new RssElementIntent(getActivity(), item, position);
        // mRootView is the view of the fragment
        final ImageView image = (ImageView) 
mRootView.findViewById(R.id.rss_element_image);
        image.setTransitionName(getString(R.string.transition_name_rss_feed_picture) + position);
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                        getActivity(), image, RssElementFragment.EXTRA_IMAGE);
        ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
    }
});

Here is the code in the target fragment:

rssImage.setTransitionName(getString(R.string.transition_name_rss_feed_picture) + getArguments().getInt(ARG_ELEMENT_POSITION));
ViewCompat.setTransitionName(rssImage, EXTRA_IMAGE);

And here is the themes.xml :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/theme_primary</item>
        <item name="colorPrimaryDark">@color/theme_primary_dark</item>
        <item name="android:colorAccent">@color/theme_accent</item>

        <!-- enable window content transitions -->
        <item name="android:windowContentTransitions">true</item>

        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>

        <!-- specify shared element transitions -->
        <item name="android:windowSharedElementEnterTransition">
            @transition/change_image_transform
        </item>
        <item name="android:windowSharedElementExitTransition">
            @transition/change_image_transform
        </item>

    </style>

Edit:

I found my error, I was getting the ImageView from the fragment rootView which was giving me a random ImageView from my RecyclerView, which mean the wrong transition name. So I used the view given by onItemClick and it worked! I also move the setTransitionName() inside the adapter.

mAdapter.setOnItemClickListener(new RssItemAdapter.OnItemCLickListener(){
        @Override
        public void onItemClick(View view, int position) {
            final RssItem item = mAdapter.getItem(position);
            final RssElementIntent intent = new RssElementIntent(getActivity(), item, position);
            // WRONG !
            // final ImageView image = (ImageView) mRootView.findViewById(R.id.rss_element_image);
            // Correct
            final ImageView image = (ImageView) view.findViewById(R.id.rss_element_image);

            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                            getActivity(), image, RssElementFragment.EXTRA_IMAGE);
            ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
        }
    });

解决方案

I found my error, I was getting the ImageView from the fragment rootView which was giving me a random ImageView from my RecyclerView, which mean the wrong transition name. So I used the view given by onItemClick and it worked! I also move the setTransitionName() inside the adapter.

mAdapter.setOnItemClickListener(new RssItemAdapter.OnItemCLickListener(){
        @Override
        public void onItemClick(View view, int position) {
            final RssItem item = mAdapter.getItem(position);
            final RssElementIntent intent = new RssElementIntent(getActivity(), item, position);
            // WRONG !
            // final ImageView image = (ImageView) mRootView.findViewById(R.id.rss_element_image);
            // Correct
            final ImageView image = (ImageView) view.findViewById(R.id.rss_element_image);

            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                            getActivity(), image, RssElementFragment.EXTRA_IMAGE);
            ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
        }
    });

这篇关于共享单元过渡没有预期的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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