android-fragments 在白色背景上转换的新片段,而不是旧片段

yws3nbqq  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(153)

我正在尝试使用此动画在两个片段之间进行过渡。
我遇到的问题是当我使用下面的代码进行过渡时。动画在白色背景上滑入新片段而不是旧片段。
我想要么使用.replace,要么创建与replace完全相同的功能。我还想让动画在旧片段上滑动新片段。

FragmentTransaction transaction = MainActivity.instance.getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_right);

    transaction.replace(R.id.main_frame_content, fragment);
    transaction.commit();

    try {
        fragmentManager.executePendingTransactions();
    } catch (Exception e) {
    }

动画:
向右滑动:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="300"
    android:fromXDelta="100%p"
    android:toXDelta="0" />
</set>

向右滑出:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="0"
    android:toXDelta="100%p" />
</set>
x0fgdtte

x0fgdtte1#

棘手的部分是你必须定义所有的Anim字段的动作。

<fragment
    android:id="@+id/profileFragment"
    android:name="sample.presentation.view.fragments.profile.ProfileFragment"
    android:label="fragment_profile"
    tools:layout="@layout/fragment_profile" >
   
    <action
        android:id="@+id/action_profileFragment_to_personalInfoFragment"
        app:destination="@id/personalInfoFragment"
        app:enterAnim="@android:anim/slide_in_left"
        app:exitAnim="@android:anim/slide_out_right"
        app:popEnterAnim="@android:anim/slide_in_left"
        app:popExitAnim="@android:anim/slide_out_right"/>
</fragment>

相关问题