【问题标题】:Shared Element Transition on RecyclerViewRecyclerView 上的共享元素转换
【发布时间】:2016-08-17 22:17:01
【问题描述】:

我有一个包含 RecyclerView 的片段(片段本身位于 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 适配器和 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的customview布局代码

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

【问题讨论】:

  • 你设置了android:transitionName ??
  • 是的,我添加了过渡名称
  • 发布您的布局代码
  • 编辑帖子并添加xml代码
  • 你发现我的代码有什么错误吗

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


【解决方案1】:

尝试膨胀默认移动过渡android.R.transition.move

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

【讨论】:

  • 仍然看不到过渡。
  • 你在哪个安卓版本上测试??
  • 我正在尝试 api 22
  • 我认为是因为viewpager
  • 我确认这是因为 viewpager..i have asked the question here
猜你喜欢
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多