Android转换不起作用

时间:2015-03-03 07:38:43

标签: android transitions

我有两个场景:

Scene1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        style="@style/AppTheme.TopPanel"
        android:id="@+id/top_panel"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </RelativeLayout>

    <RelativeLayout
        style="@style/AppTheme.Panel"
        android:id="@+id/bottom_panel"
        android:layout_width="match_parent"
        android:layout_height="72dp">
    </RelativeLayout>

</LinearLayout>

Scene2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        style="@style/AppTheme.TopPanel"
        android:id="@+id/top_panel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </RelativeLayout>

    <RelativeLayout
        style="@style/AppTheme.Panel"
        android:id="@+id/bottom_panel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </RelativeLayout>

</LinearLayout>

而且我还有一个包含唯一片段的活动。程序开始时,我用我的FirstFragment类替换这个片段。在里面我有一个按钮。单击它时,将执行以下代码:

@Override
public void onClick(View v) {
    ChangeBounds transitions = new ChangeBounds();
    transition.addListener(new Transition.TransitionListener() {
        //Other overrides...
        @Override
        public void onTransitionEnd(Transition transition) {
            SecondFragment fragment = new SecondFragment();
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        }
        //Other overrides...
    });
    ViewGroup sceneRoot = (ViewGroup) getView();
    Scene from = Scene.getSceneForLayout(sceneRoot, R.layout.scene1, getActivity());
    Scene to = Scene.getSceneForLayout(sceneRoot, R.layout.scene2, getActivity());
    from.enter();
    TransitionManager.beginDelayedTransition(sceneRoot, transitions);
    to.enter();
}

但过渡不会发生。它开始,下一刻结束。这里有效:

enter image description here

我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

在实现第一个和第二个场景之间的转换之前,首先需要添加一些代码以将布局从layout文件加载到Scene实例中。保留在SceneTransitionsActivity.java文件中。


可以这样试试:

package com.example.scenetransitions;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionManager;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;

public class SceneTransitionsActivity extends Activity {

ViewGroup rootContainer;
Scene scene1;
Scene scene2;
Transition transitionMgr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scene_transitions);

    rootContainer = (ViewGroup) findViewById(R.id.rootContainer);

    transitionMgr = TransitionInflater.from(this)
            .inflateTransition(R.transition.transition);

    scene1 = Scene.getSceneForLayout(rootContainer,R.layout.scene1_layout, this);

    scene2 = Scene.getSceneForLayout(rootContainer,R.layout.scene2_layout, this);

    scene1.enter();
}

public void goToScene2 (View view)
{
    TransitionManager.go(scene2, transitionMgr);
}

public void goToScene1 (View view)
{
    TransitionManager.go(scene1, transitionMgr);
}
.
.
.
}

参考:this&amp; this