2017-02-23 63 views
2

我想在我的應用程序中實現共享轉換。我想要RecyclerView中的ImageView將出現在下一個片段中,以便從RecyclerView共享轉換爲片段。但它不起作用。這是我做到的。共享轉換不工作recyclerview到片段

回收站的項目佈局

<?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> 

片段佈局

<?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> 

下面是我用

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(); 
     } 

這裏的java代碼是Fragment代碼

@Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     postponeEnterTransition(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move)); 
     } 
    } 
private void loadData() { 
    String title = null; 
    String description = null; 
    String urlToImage = null; 
    String url = null; 
    try { 
     title = getArguments().getString(ARG_PARAM1); 
     description = getArguments().getString(ARG_PARAM2); 
     url = getArguments().getString(ARG_PARAM3); 
     urlToImage = getArguments().getString(ARG_PARAM4); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    if (title != null) 
     tvTitle.setText(title); 
    if (tvDescription != null) 
     tvDescription.setText(description); 
    if (urlToImage != null) 
     Picasso.with(getActivity()).load(urlToImage).into(ivCover, new Callback() { 
      @Override 
      public void onSuccess() { 
       startPostponedEnterTransition(); 
      } 

      @Override 
      public void onError() { 
       startPostponedEnterTransition(); 
      } 
     }); 
} 

這裏是風格

<!-- 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> 

我到底做錯了什麼?請在這裏指導我。由於

回答

0

在你onBindViewHolder,添加以下行:

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

每個共享的元素應該有一個唯一的轉換名稱。

所以不是.addSharedElement(cover, "cover")

.addSharedElement(cover, imageTransitionName)

其中,

imageTransitionName = rv_imageView.getTransitionName(); 

,並根據您的實現(onClickListener需要view.findViewById()代替getActivity() ...),

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

您也可以嘗試fadeslide_right或您自己的自定義轉換。

所有這一切都將在該方案的工作,當你點擊一個項目,一個新的詳細的片段出現

所以你需要發送imageTransitionName在包...並獲得細節這樣的說法(說)片段,就像

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

它以某種方式工作,但當我按回按鈕。它不像我打開片段時那樣動畫。我的意思是在後面沒有動畫,但我想保留在開始時發生的事情。 –

+0

回收動畫在recyclerview-v7:25.1.1中無法正常工作。所以請耐心等待,直到Google解決。雖然你仍然可以嘗試添加一行setSharedElementReturnTransition ... –

+0

如果它是值得的,請接受答案,併爲此榮幸。 –