2017-02-23 7 views
2

Ich versuche, geteilte Übergang in meiner App zu implementieren. Ich möchte ImageView in RecyclerView, das im nächsten Fragment vorhanden sein wird, um den Austausch von RecyclerView zu fragmentieren. Aber es funktioniert nicht. Hier ist, wie ich es gemacht habe.Geteilte Transition funktioniert nicht recyclerview zu fragment

Recycler Stück Layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/iv_cover" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:scaleType="centerCrop" 
     android:transitionName="cover" 
     android:src="@drawable/ic_check_circle" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:background="#30000000" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="150dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@id/tv_title" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:background="@color/colorPrimary" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:text="BBC" 
      android:textAppearance="@style/Base.TextAppearance.AppCompat.Caption" 
      android:textColor="#fff" 
      android:textSize="16dp" /> 

     <TextView 
      android:id="@id/tv_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:background="@color/colorPrimary" 
      android:maxLines="3" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:text="NEWS TITLE" 
      android:textAppearance="@style/TextAppearance.AppCompat.Caption" 
      android:textColor="#fff" 
      android:textSize="20sp" /> 
    </RelativeLayout> 
</RelativeLayout> 

Fragment Layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/iv_cover" 
     android:transitionName="cover" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:scaleType="centerCrop" /> 

    <TextView 
     android:id="@+id/tv_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/tv_description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btn_story" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Read Full Story" 
     android:layout_gravity="center"/> 
</LinearLayout> 

Hier ist der Java-Code, die ich verwendet

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      //ImageView ivCover = (ImageView) getActivity().findViewById(R.id.iv_cover); 
      getActivity().getSupportFragmentManager() 
        .beginTransaction() 
        .replace(android.R.id.content, feedFragment, "Feed") 
        .addSharedElement(cover, "cover") 
        .addToBackStack("Feed") 
        .commit(); 
     } 

Hier ist die Fragment-Code

Hier

ist der Stil

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:windowContentTransitions">true</item> 
</style> 

Was mache ich falsch? Bitte führe mich hierhin. Dank

Antwort

0

In Ihrem onBindViewHolder, fügen Sie die folgende Zeile:

holder.ivCover.setTransitionName("trans_image" + position); 

Jedes Gemeinschafts-Element einen eindeutigen Übergang Namen haben sollte.

Also statt .addSharedElement(cover, "cover"),

Schreib .addSharedElement(cover, imageTransitionName)

wo

imageTransitionName = rv_imageView.getTransitionName(); 

und je nach Ausführung (onClickListener müssen view.findViewById() statt getActivity(). ..),

ImageView rv_imageView =(ImageView) getActivity().findViewById(R.id.iv_cover);

Sie können auch versuchen fade oder slide_right oder Ihre eigenen benutzerdefinierten Übergang.

All dies wird in dem Szenario arbeiten, wenn Sie ein Element klicken und ein neues detailliertes Fragment erscheint

Sie müssen also imageTransitionName in einem Bündel senden ... und dieses Argument bekommt in Details (sagen sie) Fragment, wie

String imageTransitionName = bundle.getString("TRANS_NAME"); 
view.findViewById(R.id.iv_cover).setTransitionName(imageTransitionName); 
+0

Es funktioniert irgendwie, aber wenn ich zurück Knopf drücke. Es ist nicht animiert wie beim Öffnen des Fragments. Ich meine auf dem Rücken drücken keine Animation ist da, aber ich will Reserve von dem, was in Start passiert ist. –

+0

Die Rückgabe-Animation funktioniert irgendwie nicht in recyclerview-v7: 25.1.1. Also ertrage es oder warte, bis es von Google repariert wird. Obwohl Sie es immer noch versuchen können, indem Sie eine weitere Zeile setSharedElementReturnTransition hinzufügen ... –

+0

Und wenn es sich gelohnt hat, bitte akzeptieren Sie die Antwort und machen Sie die Ehre. –

Verwandte Themen