RecyclerView上的共享元素转换

时间:2016-08-17 10:40:37

标签: android android-fragments android-recyclerview shared-element-transition

我有一个包含RecyclerView的Fragment(Fragment本身在ViewPager中)。 RecyclerView包含一个ImageView,而另一个Fragment也包含一个ImageView,我想要对这些ImageView进行一个sharedElement转换。我尝试了一切,但它不起作用,即使我生成了这里显示的副本here,但没有什么是工作。 我的代码是:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_album, container, false);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    albumlistClick = (AlbumlistClick) getActivity();
    albumRecycle = (RecyclerView) view.findViewById(R.id.albumRecycle);
    albumRecyclerAdapter = new AlbumRecyclerAdapter(getContext());
    layoutManager = new GridLayoutManager(getContext(), 2);
    albumRecycle.setLayoutManager(layoutManager);
    albumRecycle.setAdapter(albumRecyclerAdapter);}

RecyclerView Adapter和ViewHolder

private class AlbumRecyclerAdapter extends RecyclerView.Adapter<AlbumViewHolder> {

    Context context;
    LayoutInflater inflater;
    ArrayList<SongDB> albumList = MainActivity.AlbumList;

    public AlbumRecyclerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.custom_album_list, parent, false);
        AlbumViewHolder albumViewHolder = new AlbumViewHolder(view);
        return albumViewHolder;
    }

    @Override
    public void onBindViewHolder(final AlbumViewHolder holder,final int position) {
        SongDB curr = albumList.get(position);
        holder.album_name.setText(curr.getAlbum());
        holder.album_artist.setText(curr.getArtist());
        Glide.with(context)
                .load("file://" + curr.getAlbumDB().getAlbumArt())
                //.load(R.drawable.image)
                .error(R.drawable.play_icon)
                .centerCrop()
                .crossFade()
                .into(holder.album_image);

            ViewCompat.setTransitionName(holder.album_image,String.valueOf(position) + "_albumart");


    }

    @Override
    public int getItemCount() {
        return albumList.size();
    }


}

private class AlbumViewHolder extends RecyclerView.ViewHolder {
    ImageView album_image;
    TextView album_name, album_artist;

        public AlbumViewHolder(final View itemView) {
        super(itemView);

        album_image = (ImageView) itemView.findViewById(R.id.artist_image);
        album_name = (TextView) itemView.findViewById(R.id.album_name);
        album_artist = (TextView) itemView.findViewById(R.id.album_artist);
        itemView.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                        {


                            int data = albumRecycle.getChildAdapterPosition(itemView);
                            Trail album_list = new Trail(data);

                            Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.transition);

                            album_list.setSharedElementEnterTransition(transition);
                            album_list.setSharedElementReturnTransition(transition);

                            //album_image = (ImageView) itemView.findViewById(R.id.artist_image);

                            getActivity().getSupportFragmentManager()
                                    .beginTransaction()
                                    .replace(R.id.albumlistContainer, album_list)
                                    .addToBackStack(null)
                                    .addSharedElement(album_image, "albumart")
                                    .commit();
                        }

                    }
                }
        );
    }
}

其他活动的布局代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true"
tools:context="com.example.vitymspaul.asdfg.Trail">

<!-- TODO: Update blank fragment layout -->

<ImageView
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:id="@+id/artist_image"
    android:layout_gravity="center"
    android:scaleType="centerCrop"
    android:transitionName="albumart"
    android:clickable="true"
    tools:ignore="ContentDescription,UnusedAttribute"/>

recyclerview的自定义视图布局代码

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="180dp"
android:layout_height="230dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
app:cardElevation="6dp">
<ImageView
    android:id="@+id/artist_image"
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:layout_above="@+id/imagebelow"
    android:layout_gravity="center_horizontal|top"
    android:scaleType="centerCrop"
    tools:ignore="ContentDescription"
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">



    <RelativeLayout
        android:id="@+id/imagebelow"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal|bottom"
        android:background="#ffe3e3">

        <TextView
            android:id="@+id/album_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="20dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#0a0000"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/album_artist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/album_name"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#828282" />
    </RelativeLayout>

</RelativeLayout>

转换文件

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds />
    <changeTransform />
</transitionSet>

1 个答案:

答案 0 :(得分:0)

尝试充气默认移动转换android.R.transition.move

Transition transition = TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move);
album_list.setSharedElementEnterTransition(transition);
album_list.setSharedElementReturnTransition(transition);