使用片段共享转换时,返回转换无法正常工作 [英] Return transition not working correctly when using fragment shared transitions

查看:70
本文介绍了使用片段共享转换时,返回转换无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个片段 ListMovieFragment DetailMovieFragment .

我在 ListMovieFragment 中有一个接口,该接口在 MainActivity 中实现.我正在使用共享元素过渡;当我单击 ListMovieFragment 中的图像视图时,在 MainActivity 中调用 onMovieSelected .

I have an interface in ListMovieFragment that is implemented in the MainActivity. I am using shared element transition ; when I click the image view in ListMovieFragment the onMovieSelected is called in the MainActivity.

ListMovieFragment 进行转换是可行的.
但是,当我单击 back 按钮时,从 DetailMovieFragment 到ListMovieFragment的转换无法正常工作.

Transitioning from the ListMovieFragment works.
But when I click the back button, transitioning from the DetailMovieFragment to the ListMovieFragment fails to work.

这里是MainActivity.我想我在片段上设置过渡时使用了错误的组合.

Here is the MainActivity. I think I have got an incorrect combination for setting the transitions on the fragments.

public class MainActivity extends AppCompatActivity implements ListMovieFragment.MovieSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(savedInstanceState == null) {
            ListMovieFragment listMovieFragment = new ListMovieFragment();

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.activity_main, listMovieFragment, ListMovieFragment.TAG);
            fragmentTransaction.commit();
        }
    }

    @Override
    public void onMovieSelected(int movieId) {
        DetailMovieFragment detailMovieFragment =
                (DetailMovieFragment)getSupportFragmentManager().findFragmentByTag(DetailMovieFragment.TAG);
        /* Create a new DetailMovieFragment if not exits */
        if(detailMovieFragment == null) {
            detailMovieFragment = new DetailMovieFragment();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            /* Get the fragments that will be using the transition */
            ListMovieFragment listMovieFragment =
                    (ListMovieFragment)getSupportFragmentManager().findFragmentByTag(ListMovieFragment.TAG);
            if(listMovieFragment == null) {
                listMovieFragment = new ListMovieFragment();
            }

            /* Inflate the transition */
            Transition changeTransition = TransitionInflater
                    .from(MainActivity.this)
                    .inflateTransition(R.transition.change_image_transform);

            /* source fragment (ListMovieFragment) */
            listMovieFragment.setExitTransition(new Explode());
            listMovieFragment.setSharedElementReturnTransition(changeTransition);

            /* Destination fragment (DetailMovieFragment) */
            detailMovieFragment.setSharedElementEnterTransition(changeTransition);
            detailMovieFragment.setEnterTransition(new Explode());

            /* Get the shared imageview from the source fragment (MovieListFragment) */
            final ImageView ivSharedImage = (ImageView) findViewById(R.id.ivMoviePoster);

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.activity_main, detailMovieFragment, DetailMovieFragment.TAG);
            fragmentTransaction.addToBackStack(DetailMovieFragment.TAG);
            fragmentTransaction.addSharedElement(ivSharedImage, getResources().getString(R.string.transition_poster_image));
            fragmentTransaction.commit();
        }
        else {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.activity_main, detailMovieFragment, DetailMovieFragment.TAG);
            fragmentTransaction.addToBackStack(DetailMovieFragment.TAG);
            fragmentTransaction.commit();
        }
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        }
        else {
            super.onBackPressed();
        }
    }
}

我的过渡xml文件:

<transitionSet>
    <changeBounds />
</transitionSet>

片段列表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="18dp"
    android:paddingBottom="6dp">

    <ImageView
        android:id="@+id/ivMoviePoster"
        android:layout_width="184dp"
        android:layout_height="276dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:transitionName="@string/transition_poster_image"/>
</LinearLayout>

fragment_detail

fragment_detail

<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"
    tools:context="me.androidbox.fragmenttransitions.detail.DetailMovieFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="The Movie App"
        android:textSize="28sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/holo_blue_dark"/>

    <ImageView
        android:id="@+id/ivMoviePoster"
        android:layout_width="92dp"
        android:layout_height="138dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="112dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:layout_gravity="end"
        android:transitionName="@string/transition_poster_image"/>
</FrameLayout>

activity_main

activity_main

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="me.androidbox.fragmenttransitions.activity.MainActivity">
</FrameLayout>

推荐答案

您应如下更改 fragment_list.xml ,更改 android:layout_width/height 属性.

You should change fragment_list.xml as below, android:layout_width/height properties are changed.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="18dp"
    android:paddingBottom="6dp">

    <ImageView
        android:id="@+id/ivMoviePoster"
        android:layout_width="184dp"
        android:layout_height="276dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:transitionName="@string/transition_poster_image"/>
</LinearLayout>

这篇关于使用片段共享转换时,返回转换无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆