共享元素转换在Fragment中不起作用

时间:2018-05-31 10:15:50

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

需要动画。

Fragment one中有一个recyclelerViewholder,其行项如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">
    <LinearLayout
        android:id="@+id/badge_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="32dp"
        android:orientation="vertical"

        >
    <ImageView
        android:layout_width="64dp"
        android:layout_height="74dp"
        android:id="@+id/iv_acheivements"
        android:src="@drawable/badge_image"
        android:transitionName="@string/badge_image_fragment_transition"
        tools:ignore="UnusedAttribute"
        />
    <com.sharesmile.share.views.LBTextView
        android:layout_width="64dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/tv_acheivements_title"
        android:textSize="11sp"
        android:textColor="@color/black_53"
        android:layout_marginTop="13dp"
        android:text="Change Maker"
        />
    </LinearLayout>
</LinearLayout>

点击badge_layout后,我使用以下代码启动片段B:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <ImageView
        android:id="@+id/close_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_close_black_24dp"
        android:layout_gravity="right"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="3dp"
        android:visibility="invisible"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="50dp"
        android:paddingRight="50dp">
    <com.sharesmile.share.views.LBTextView
        android:id="@+id/congratulation_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/congratulations"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/clr_5d"
        android:textSize="24dp"
        />
    <com.sharesmile.share.views.LRTextView
        android:id="@+id/badge_earned_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/clr_5d"
        android:text="@string/upgraded_your_badge_txt"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:layout_marginTop="5dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="150dp"
            android:layout_height="180dp"
            android:id="@+id/iv_badge"
            android:src="@drawable/badge_image"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:transitionName="@string/badge_image_fragment_transition"
            tools:ignore="UnusedAttribute"
            />
        <com.sharesmile.share.views.LBKTextView
            android:id="@+id/badge_title_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="@color/black_57"
            tools:text="CAPTAIN PLANET"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            />
        <com.sharesmile.share.views.LRTextView
            android:id="@+id/badge_amount_raised_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="16sp"
            tools:text="Thanks for raising \u20B9 500 for our environment"
            android:textColor="@color/greyish_brown_two"
            android:layout_marginBottom="10dp"
            />
        <com.sharesmile.share.views.LRTextView
            android:id="@+id/badge_upgrade_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="16sp"
            tools:text="Badge Upgrade : Raise \u20B91500 more"
            android:textColor="@color/greyish_brown_two"
            />

    </LinearLayout>
    <com.sharesmile.share.views.MSBButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tell_your_friends"
        android:background="@drawable/rounded_corner"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:layout_gravity="center_horizontal"
        android:paddingTop="14dp"
        android:paddingBottom="14dp"
        android:id="@+id/btn_tell_your_friends"
        android:layout_marginBottom="20dp"
        />
    <com.sharesmile.share.views.LBTextView
        android:id="@+id/continue_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/badge_continue"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:visibility="visible"
        />
    </LinearLayout>
</LinearLayout>

片段a中的代码是:

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.addSharedElement(sharedView, ViewCompat.getTransitionName(sharedView));
            if(getSupportFragmentManager().findFragmentByTag(tag)==null) {
                fragmentTransaction.replace(getFrameLayoutId(), fragmentToBeLoaded,
                        tag);

                if (addToBackStack) {
                    fragmentTransaction.addToBackStack(tag);
                }
                if (allowStateLoss) {
                    fragmentTransaction.commitAllowingStateLoss();
                } else {
                    fragmentTransaction.commit();
                }

第二个片段是:

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
        }
    }

导入片段:(如果这是您要检查的内容)

import android.support.v4.app.Fragment;
import android.view.View;

但它不起作用。知道为什么这不起作用。

0 个答案:

没有答案