片段上的SharedElementTransition不起作用

时间:2017-02-06 19:04:27

标签: java android animation fragment transition

我想将共享元素转换添加到recyclerview中的项目,这些项目将在另一个片段上打开,但没有任何作用。

我想分享的唯一视图是ImageView。

Recyclerview项目中的ImageViews和Textview使用Glide加载图像,使用firebase数据库方法加载文本。在详细片段中,将传递recyclerview项目的位图,并且只会再次下载文本。

从recyclerview项目中打开新片段的方法是:

homeUserAnnouncesAdapter.setOnCardAnnounceClickedListener(new OnCardAnnounceClickedListener() {
        @Override
        public void onCardClicked(CardRentAnnounce card, ImageView imageView, int poz) {
            Log.v("TRANSITIONTEST", "CLICKED FROM HOME FRAGMENT --- RENT");
            Log.v("TRANSITIONTEST", "in onClick: " + imageView.getTransitionName());

            Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

            OpenRentAnnounceFragment newFragment = OpenRentAnnounceFragment.newInstanceWithImage(card, bitmap, poz);

            newFragment.setSharedElementEnterTransition(new DetailsTransition());
            newFragment.setEnterTransition(new Fade());
            newFragment.setExitTransition(new Fade());
            newFragment.setSharedElementReturnTransition(new DetailsTransition());

            getActivity().getSupportFragmentManager()
                    .beginTransaction()
                    .addSharedElement(imageView, imageView.getTransitionName())
                    .replace(R.id.content_main_without_toolbar, newFragment)
                    .addToBackStack(null)
                    .commit();
        }
    });

DetailsTransition类是:

public class DetailsTransition extends TransitionSet{

    public DetailsTransition() {
        init();
    }

    /**
     * This constructor allows us to use this transition in XML
     */
    public DetailsTransition(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOrdering(ORDERING_TOGETHER);
        addTransition(new ChangeBounds())
                .addTransition(new ChangeTransform())
                .addTransition(new ChangeImageTransform());
    }
}

我在适配器上为每个视图分配不同的转换名称:

@Override
public void onBindViewHolder(final HomeUserAnnouncesViewHolder holder, int position) {
    final CardRentAnnounce cardRentAnnounce = cardRentAnnounceArrayList.get(position);
    holder.updateUI(cardRentAnnounce);

    ViewCompat.setTransitionName(holder.rentImage, String.valueOf(position) + "_rent");
    MaterialRippleLayout mlr = (MaterialRippleLayout)holder.itemView.findViewById(R.id.search_home_ripple);
    mlr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(listener != null){
                listener.onCardClicked(cardRentAnnounce, holder.rentImage, holder.getAdapterPosition());
            }
        }
    });
}

recyclelerview项目中的图像ID与片段中的图像相同

我已经按照本教程创建了转换,甚至将项目克隆到我的电脑上,在模拟器上运行并尝试完全像在工作项目中但仍然无法正常工作。

我如何在新片段中设置图像和imageTransition名称:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_open_rent_announce, container, false);
    rootView = view;
    image = (ImageView)view.findViewById(R.id.detailed_image);
    image.setImageBitmap(currentImageBitmap);
    image.setTransitionName(String.valueOf(pozition) + "_rent");
    Log.v("TRANSITIONTEST", "in onCreateView: " + image.getTransitionName());
    initViews(view);
    setViews();
    return view;
}

Fragment SharedElementsTransition tutorial

在recyclerview和详细片段上的两个图像都加载了Glide,但我尝试使用本地图像但仍然没有成功。 我错过了什么?如果需要,请求更多代码。 感谢。

完整适配器:

package com.minimalart.studentlife.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.balysv.materialripple.MaterialRippleLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.minimalart.studentlife.R;
import com.minimalart.studentlife.interfaces.OnCardAnnounceClickedListener;
import com.minimalart.studentlife.interfaces.OnImageReadyListener;
import com.minimalart.studentlife.models.CardRentAnnounce;

import java.util.ArrayList;

/**
 * Created by ytgab on 23.01.2017.
 */

public class HomeUserAnnouncesAdapter extends RecyclerView.Adapter<HomeUserAnnouncesAdapter.HomeUserAnnouncesViewHolder>{

    public OnCardAnnounceClickedListener listener;

    public OnImageReadyListener imageListener;
    private ArrayList<CardRentAnnounce> cardRentAnnounceArrayList;
    private Context context;

    public void setOnCardAnnounceClickedListener(OnCardAnnounceClickedListener listener){
        this.listener = listener;
    }

    public void setOnImageReadyListener(OnImageReadyListener imageListener) {
        this.imageListener = imageListener;
    }

    public HomeUserAnnouncesAdapter(ArrayList<CardRentAnnounce> cardRentAnnounceArrayList, Context context) {
        this.cardRentAnnounceArrayList = cardRentAnnounceArrayList;
        this.context = context;
    }

    @Override
    public HomeUserAnnouncesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View announceCard = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_home_user_announces, parent, false);

        return new HomeUserAnnouncesAdapter.HomeUserAnnouncesViewHolder(announceCard);
    }

    @Override
    public void onBindViewHolder(final HomeUserAnnouncesViewHolder holder, int position) {
        final CardRentAnnounce cardRentAnnounce = cardRentAnnounceArrayList.get(position);
        holder.updateUI(cardRentAnnounce);

        ViewCompat.setTransitionName(holder.rentImage, String.valueOf(position) + "_rent");
        MaterialRippleLayout mlr = (MaterialRippleLayout)holder.itemView.findViewById(R.id.search_home_ripple);
        mlr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null){
                    listener.onCardClicked(cardRentAnnounce, holder.rentImage, holder.getAdapterPosition());
                }
            }
        });
    }

    public void loadNewData(ArrayList<CardRentAnnounce> list){
        cardRentAnnounceArrayList = list;
    }

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

    public class HomeUserAnnouncesViewHolder extends RecyclerView.ViewHolder{

        private TextView rentTitle;
        private TextView rentRooms;
        private TextView rentPrice;
        private TextView rentLocation;
        private ImageView rentImage;
        private Uri uri;
        StorageReference storageReference;

        private static final String REF_RENT_IMAGES = "rent-images";

        public HomeUserAnnouncesViewHolder(View itemView) {
            super(itemView);
            rentTitle = (TextView)itemView.findViewById(R.id.card_home_user_announces_title);
            rentImage = (ImageView)itemView.findViewById(R.id.detailed_image);
        }

        public void updateUI(CardRentAnnounce cardRentAnnounce){
            getImageURL(cardRentAnnounce.getAnnounceID());
            rentTitle.setText(cardRentAnnounce.getTitle());
        }

        public void getImageURL(String announceID){
            FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
            storageReference = firebaseStorage.getReference().child(REF_RENT_IMAGES).child(announceID);
            /*rentImage.setImageDrawable(context.getResources().getDrawable(R.drawable.apartment_inside, context.getTheme()));*/
            Glide.with(context).using(new FirebaseImageLoader()).load(storageReference).into(new GlideDrawableImageViewTarget(rentImage){
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                    super.onResourceReady(resource, animation);
                    Log.v("TRANSITIONTEST", "Image ready");
                    if(imageListener != null)
                        imageListener.onImageReady();
                }
            });
        }

        public ImageView getRentImage(){
            return rentImage;
        }

        public Uri getUri() {
          return uri;
        }

        public void setUri(Uri uri) {
            this.uri = uri;
        }
    }
}

recyclerview卡布局:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/card_home_user_announces"
card:cardElevation="@dimen/card_elevation"
card:cardCornerRadius="@dimen/card_radius">

<com.balysv.materialripple.MaterialRippleLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/search_home_ripple">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@drawable/apartment_inside"
            android:id="@+id/detailed_image"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:ellipsize="marquee"
            android:id="@+id/card_home_user_announces_title"
            android:text="Titlu"
            android:textColor="@color/textPrincipal"
            android:maxLines="1"
            android:lines="1"
            android:textSize="16sp"
            android:textStyle="normal"/>
    </LinearLayout>

</com.balysv.materialripple.MaterialRippleLayout>

</android.support.v7.widget.CardView>

详细的片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.minimalart.studentlife.fragments.OpenRentAnnounceFragment">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/rent_appbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/rent_detailed_collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:descendantFocusability="blocksDescendants">

            <ImageView
                android:id="@+id/detailed_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:src="@drawable/apartment_inside"
                app:layout_collapseMode="parallax"
                android:transitionName="rentTransition"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/detailed_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                app:title="Titlu">
            </android.support.v7.widget.Toolbar>

            <ImageButton
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_gravity="left"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginTop="@dimen/standard_padding"
                android:src="@drawable/ic_arrow_back"
                android:background="@color/full_alpha"
                android:id="@+id/detailed_back_btn"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        app:behavior_overlapTop="30dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/standard_padding"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/detailed_description"
                    android:textColor="@color/textSecondary"
                    android:textSize="16sp"
                    android:text="Descriere: "
                    android:padding="@dimen/activity_horizontal_margin" />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/standard_padding"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/detailed_rooms"
                    android:textColor="@color/textSecondary"
                    android:textSize="16sp"
                    android:text="Număr camere: "
                    android:padding="@dimen/activity_horizontal_margin" />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/standard_padding"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/detailed_location"
                    android:textColor="@color/textSecondary"
                    android:textSize="16sp"
                    android:text="Locatie: "
                    android:padding="@dimen/activity_horizontal_margin" />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/standard_padding"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/detailed_price"
                    android:textColor="@color/textSecondary"
                    android:textSize="16sp"
                    android:text="Pret: "
                    android:padding="@dimen/activity_horizontal_margin" />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/standard_padding"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding"
                android:layout_marginBottom="@dimen/standard_padding">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/detailed_discount"
                    android:textColor="@color/textSecondary"
                    android:textSize="16sp"
                    android:text="Discount studenti"
                    android:padding="@dimen/activity_horizontal_margin" />

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/standard_padding"
                android:layout_marginRight="@dimen/standard_padding"
                android:layout_marginBottom="@dimen/standard_padding">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/detailed_seller"
                        android:textSize="16sp"
                        android:text="@string/open_rent_seller"
                        android:padding="@dimen/activity_horizontal_margin" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <Button
                            android:id="@+id/detailed_contact_user_email"
                            android:background="@color/full_alpha"
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:text="@string/open_rent_email"
                            android:textStyle="bold"
                            android:textColor="@color/textSecondary"
                            android:layout_weight="1">

                        </Button>

                        <Button
                            android:id="@+id/detailed_contact_user_phone"
                            android:background="@color/full_alpha"
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:text="@string/open_rent_phone"
                            android:textStyle="bold"
                            android:textColor="@color/textSecondary"
                            android:layout_weight="1">

                        </Button>

                    </LinearLayout>

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:id="@+id/rent_detailed_fab"
        app:fabSize="normal"
        android:src="@drawable/ic_favorite"
        android:layout_gravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>

</FrameLayout>

0 个答案:

没有答案