共享元素从Gridview转换不从正确的位置启动

时间:2017-01-13 19:38:59

标签: android gridview

我有一个gridview,其中cardview在主要活动上显示ImageView。我正在尝试从图像到细节活动的共享元素转换。

以下是MainActivity的相关部分

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <GridView
        android:id="@+id/showGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:layout_margin="16dp"
        android:verticalSpacing="16dp"/>

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

项目的xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/showCard"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_marginBottom="16dp"
android:background="@android:color/white"
android:elevation="8dp"
android:foreground="?selectableItemBackground"
android:orientation="vertical">

<ImageView
    android:id="@+id/showLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/rth"
    android:transitionName="logoImage" />

DetailsActivity的xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="400dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/logo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/rth"
                android:transitionName="logoImage"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

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

这是来自MainActivity的java

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this, FeedActivity.class);

            Show show = (Show) adapterView.getItemAtPosition(i);
            intent.putExtra("selectedShow", show);

            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, (View)findViewById(R.id.showLogo), "logoImage");
            startActivity(intent, options.toBundle());

        }
    }

发生过渡效果并显示正确的图像。但是,转换始终从gridview中的第一个项开始。退出转换显示来自detailsActivity的图像返回到gridview中的第一个项目,即使它来自另一个位置。

如何从用户选择的项目开始和退出过渡?谢谢!

2 个答案:

答案 0 :(得分:0)

您需要为transitionName的每个孩子设置不同的GridView。然后,您需要找出为transitionName中的相应详细信息设置相同DetailsActivity的方法。

Great implementation on GitHub

答案 1 :(得分:0)

我知道这个问题很旧,但是对于任何需要它的人...

在线

ActivityOptionsCompat options = 
ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, 
(View)findViewById(R.id.showLogo), "logoImage");

不要使用(View),请放view.

所以不是

(MainActivity.this, (View)findViewById(R.id.showLogo), "logoImage");

使用

(MainActivity.this, view.findViewById(R.id.showLogo), "logoImage");

这是因为您需要引用在GridView中用作子视图的视图