0

I have a recycler view. Inside onclick of the recyclerview item, I have applied a shared element transition. Here's the recycler view adapter:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_location_item, parent, false);
    //final TextView locationName = (TextView)view.findViewById(R.id.locationName);
    //String transitionName = locationName.getTransitionName();
   // locationName.setTransitionName("testing");
    final ViewHolder vh = new ViewHolder(view);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = vh.getAdapterPosition();
            if( pos != RecyclerView.NO_POSITION) {

                final String cityName = mItems[pos];
                Log.d(TAG,"city name : " + cityName);
                Intent intent = new Intent(mCtx, DashboardDetailPageActivity.class);

                // Get the transition name from the string
                TextView locationName = (TextView)view.findViewById(R.id.locationName);
                String transitionName = locationName.getTransitionName();

                ActivityOptionsCompat options =

                        ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mCtx,
                                locationName,   // Starting view
                                transitionName    // The String
                        );
                //Start the Intent
                mCtx.startActivity(intent, options.toBundle());
                //transitionToActivity(DashboardDetailPageActivity.class, vh, cityName, view);
            }
        }
    });



    return vh;
}

Here's is my layout for cardview:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/locationIcon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />
<TextView
    android:id="@+id/locationName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/location"
    android:textColor="@color/white"
    android:layout_gravity="center"
    android:textSize="@dimen/textsize_18"
    android:transitionName="@string/title"
    android:textAllCaps="true"
    android:gravity="center"/>

Here's, my final Activity layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/navigationDrwaerSerachLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dash_toolbar_height"
            android:background="@drawable/city_image"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolbar_height2"
                android:minHeight="@dimen/toolbar_height2"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/cityIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:transitionName="testing" />

                <TextView
                    android:id="@+id/cityNameTextView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/location"
                    android:textAllCaps="true"
                    android:textColor="@color/white"
                    android:textSize="@dimen/textsize_18"
                    android:transitionName="@string/title" />
            </FrameLayout>
        </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:id="@+id/sample2_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/navigationDrwaerSerachLayout">


        </FrameLayout>

    </RelativeLayout>

    <com.lapism.searchview.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.lapism.searchview.SearchBehavior" />


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

<include layout="@layout/nav" />

The transition effect is not working. It direclty migrates from start activity to final activity. What's the issue here guys??

androider
  • 982
  • 6
  • 16
  • 32

3 Answers3

1

You need a unique transition name. In the case of animating from a list item, you'd want to set the view's transition name to something unique for each list item (consider using adapter position or ID of list item), and then pass that unique transition name to your target activity or fragment. In the target view, you'd find the view by id, and then set the transition name to whatever you passed in thereby linking the two views together by transition name.

There may be other issues with your code, but that's one I noticed immediately that just faced.

ejw
  • 151
  • 1
  • 5
0

Rather than making animation from the recycler view adapter, I did it from the parent activity. I guess, there must be some issue with the context

androider
  • 982
  • 6
  • 16
  • 32
0

Did you enable window content transition in Theme?

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowContentTransitions">true</item>
</style>
Kai Wang
  • 3,303
  • 1
  • 31
  • 27