简体   繁体   English

多个元素时共享元素事务无法与recyclerview一起使用

[英]shared element transaction not working with recyclerview when more than one element

there is a recyclerview in android application and i want shared element transaction when user goes to other fragment for that I tried as mentioned below code. 在Android应用程序中有一个recyclerview,当用户转到其他片段时,我想要共享元素事务,因为我尝试了以下代码。

image view in row xml file 行xml文件中的图像视图

 <ImageView
    android:id="@+id/iv_album_thumbnail"
    android:layout_width="match_parent"
    android:layout_height="160dip"
    android:layout_gravity="center"
    android:transitionName="image_transit"
    android:scaleType="centerCrop"
    tools:ignore="ContentDescription"
    android:src="@drawable/img_default"
    android:gravity="center" />

image view in second fragment 第二个片段中的图像视图

<ImageView
    android:id='@+id/iv_album_thumbnail'
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"
    android:transitionName="image_transit"
    tools:ignore="ContentDescription,UnusedAttribute"
    android:layout_marginTop="60dip"
    android:layout_alignParentTop="true"
    android:src="@drawable/placeholder" />

on click method 点击方法

public void onAlbumClicked(RecyclerViewAdapter.ViewHolder holder, int position) {

    SongListFragment kittenDetails = SongListFragment.newInstance(aList.get(position));

    // Note that we need the API version check here because the actual transition classes (e.g. Fade)
    // are not in the support library and are only available in API 21+. The methods we are calling on the Fragment
    // ARE available in the support library (though they don't do anything on API < 21)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        kittenDetails.setSharedElementEnterTransition(new DetailsTransition());
        kittenDetails.setEnterTransition(new Fade());
        setExitTransition(new Fade());
        kittenDetails.setSharedElementReturnTransition(new DetailsTransition());
    }

    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .addSharedElement(holder.imgPicture, "image_transit")
            .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
            .replace(R.id.root, kittenDetails)
            .addToBackStack(null)
            .commit();
}

when there is only one item in recyclerview it is working perfect but when there is more than one item in recyclerview there is no effect at all, can anyone please currant me. recyclerview只有一项时,它是完美的工作,但是当recyclerview有一项以上时,则根本没有效果,任何人都可以加醋。

I think the problem is that you have multiple views having the same transition name in the source view tree. 我认为问题在于源视图树中有多个具有相同过渡名称的视图。

You should set it only on clicked row : 您应该仅在点击的行上进行设置:

  • remove the android:transitionName attribute from the row xml. 从xml行中删除android:transitionName属性。
  • add ViewCompat.SetTransitionName(holder.imgPicture, "image_transit"); 添加ViewCompat.SetTransitionName(holder.imgPicture, "image_transit");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM