简体   繁体   English

Android:RecyclerView和Fragment之间没有共享元素转换

[英]Android: No shared element transition between RecyclerView and Fragment

I have the following setup: one MainActivity, one AFragment which has a RecyclerView and a BFragment which is used to contain details to elements in AFragment. 我有以下设置:一个MainActivity,一个具有RecyclerView的AFragment和一个用于包含AFragment中元素详细信息的BFragment。 When the user taps on an item in AFragment, it takes them to BFragment. 当用户点击AFragment中的项目时,它将带到BFragment。

The desired effect I want to create is to take the ImageView for the item in AFragment and transition it to BFragment. 我想要创建的理想效果是在AFragment中获取该项目的ImageView并将其过渡到BFragment。

The problem is that currently, for all items on the "first page" of the RecyclerView (by "first page" I mean all items that you can see on the screen when you first enter AFragment), the enter transition into BFragment does not work at all. 问题是,当前,对于RecyclerView“第一页”上的所有项目(按“第一页”,我是指您首次输入AFragment时可以在屏幕上看到的所有项目),进入BFragment的回车过渡不起作用完全没有 However when returning from BFragment to AFragment, the transition plays normally. 但是,从BFragment返回AFragment时,过渡会正常播放。 For all other items in the RecyclerView (ie items not on the "first page"), both the enter and exit transitions play perfectly. 对于RecyclerView中的所有其他项目(即不在“第一页”上的项目),输入和退出转换都可以完美播放。

My code 我的密码

AFragment 片段

public class AFragment extends Fragment {
    ...
    public static class ItemAdapter extends RecyclerView.Adapter<ViewHolder> {
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final Item info = items.get(position);
            ViewCompat.setTransitionName(holder.imageView, "shared_image_" + info.getId());

            holder.imageView.setImageDrawable(info.getIcon());
            holder.name.setText(info.getName());
            holder.rootView.setTag(info.getId());

            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BFragment frag = BFragment.newInstance((Integer) v.getTag());
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        frag.setSharedElementEnterTransition(new ComboTransition());
                        frag.setEnterTransition(new Fade());
                        setExitTransition(new Fade());
                        frag.setSharedElementReturnTransition(new ComboTransition());

                        Log.d(TAG, "Name: " + imageView.getTransitionName());
                    }


                    getFragmentManager().beginTransaction()
                        .addSharedElement(imageView, "shared_image")
                        .replace(R.id.content, frag)
                        .addToBackStack(null)
                        .commit();
                }
            });
        }                   
    }
    ...     
}

BFragment 片段

public class BFragment extends Fragment {
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
        ImageView imageView = (ImageView) findViewById(R.id.image_view);
        ViewCompat.setTransitionName(imageView, "shared_image");
        ...
    }
    ...     
}

Urg. 哎呀 I found the problem, which is unique to my app. 我发现了问题,这是我的应用程序特有的。 The problem is that I forgot I had another copy of the RecyclerView in BFragment (the reason being I wanted users to be able to search all items in AFragment while in BFragment so the UX is a bit better). 问题是我忘了在BFragment中有另一个RecyclerView副本(原因是我希望用户能够在BFragment中搜索AFragment中的所有项目,因此UX更好一些)。 So what has happening was that when the user exits AFragment and enters BFragment, the recyclerview in BFragment would get initialized and thus all items on the "first page" also get initialized with the same transition names as the items in AFragment (thus the odd behavior with items on the "first page"). 因此发生的事情是,当用户退出AFragment并进入BFragment时,BFragment中的recyclerview将被初始化,因此“首页”上的所有项目也都将使用与AFragment中的项目相同的过渡名称进行初始化(因此,异常行为以及“首页”上的项目)。 Of course this is really bad as now two items have the same transition name. 当然,这确实很糟糕,因为现在两个项目具有相同的过渡名称。

I solved the issue by changing the constructor of ItemAdapter to take in a key unique to the creator and prefixing this key to all transition names. 我通过更改ItemAdapter的构造函数以采用创建者唯一的键并将该键添加到所有过渡名称的前缀来解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android共享元素在带有片段的活动与带有片段的另一个活动之间转换 - Android shared element transition between an activity with a fragment to another activity with a fragment Android在具有recyclerview和详细信息视图的片段之间共享元素转换 - Android shared element transition between fragments having recyclerview & details view 使用共享元素的Android片段转换 - Android fragment transition with shared element Recyclerview和Viewpager之间的Glitchy共享元素过渡 - Glitchy shared element transition between recyclerview and viewpager 使用RecyclerView项目进行共享元素转换 - Android - Shared Element Transition with RecyclerView items - Android 片段和活动之间的ImageView共享元素过渡 - ImageView Shared Element Transition between Fragment and Activity 如何使用 Android 导航组件实现从 RecyclerView 项目到 Fragment 的共享过渡元素? - How to implement shared transition element from RecyclerView item to Fragment with Android Navigation Component? RecyclerView上的共享元素转换 - Shared Element Transition on RecyclerView RecyclerView中的共享元素转换 - Shared element transition in RecyclerView Android fragment 共享元素过渡实现 - Android fragment shared element transition implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM