Android活动和片段转换动画

时间:2018-02-05 03:35:30

标签: android animation android-activity android-transitions fragmentmanager

我有两个活动,它们都会在其活动布局中初始化一个片段。但是,我无法将活动A中的动画添加到活动B.转换工作正常。这是一些代码片段。

我尝试过使用overridePending,它几乎什么都不做。另外,我尝试通过fragmentManager中的.setCustomAnimations()添加自定义动画。但是,我只能看到第二个动画幻灯片,但没有滑出。第一项活动根本没有动画。

主要活动(活动A):

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null){
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, new MainFragment())
                    .commit();
        }
    }

    public void gotToCommentActivityWithPostId(String postId){
        Intent intent = new Intent(this, CommentActivity.class);
        Bundle b = new Bundle();
        b.putString(CommentActivity.POST_ID_KEY, postId);
        intent.putExtras(b);
        startActivity(intent);
    }
}

主要活动布局:

    <android.support.constraint.ConstraintLayout 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="yaojungyang.testing.Screens.MainScreen.MainActivity">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

</android.support.constraint.ConstraintLayout>

目的地活动(活动B):

public class CommentActivity extends AppCompatActivity {
    public static final String POST_ID_KEY = "POST_ID_KEY";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment_activity_layout);
        if(savedInstanceState == null){
            Bundle bundle = new Bundle();
            bundle.putString(POST_ID_KEY, getIntent().getStringExtra(POST_ID_KEY));
            Fragment fragment = new CommentFragment();
            fragment.setArguments(bundle);
            getSupportFragmentManager()
                    .beginTransaction()
                    .setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_right, R.anim.slide_to_right, R.anim.slide_to_right)
                    .add(R.id.fragment_container, fragment)
                    .commit();
        }
    }
}

目标活动布局:

<android.support.constraint.ConstraintLayout 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="yaojungyang.testing.Screens.MainScreen.MainActivity">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案