Android Fragments过渡问题

时间:2017-09-22 04:46:27

标签: java android android-fragments animation fragment

使用下面的代码从一个片段HelloAfragment转换到HelloBfragment时,如何在从HelloAfragment转换为HelloBfragment时添加延迟

   TAG = HelloFrament.class.getSimpleName();
   fragmentManager.beginTransaction()
           .setCustomAnimations(R.anim.slide_in_left,R.anim.slide_out_right)
            .replace(HelloAfragment.getId(), HelloBfragment, TAG)
            .addToBackStack(TAG)
            .commit();

3 个答案:

答案 0 :(得分:0)

尝试以下代码

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            fragmentManager.addOnBackStackChangedListener(this);
            fragmentTransaction.replace(R.id.frame, firstFragment, "h");
            fragmentTransaction.addToBackStack("h");
            fragmentTransaction.commit();

答案 1 :(得分:0)

您只需要进行一些小改动,请使用slide_in_left

中的以下代码
<translate xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/linear_interpolator"
       android:fromXDelta="1000" android:toXDelta="0"
       android:duration="700"/>

此代码为slide_in_right

<translate xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       android:fromXDelta="0" android:toXDelta="-1000"
       android:duration="700"/>

答案 2 :(得分:0)

我无法实现两个片段之间的转换,所以我最终将目标片段更改为activity并使用以下代码进行活动

       View imageView = findViewById(R.id.imageView);
    View textView = findViewById(R.id.textView);
    View button = findViewById(R.id.button);

    Intent intent = new Intent(this, EndActivity.class);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        textView.setTransitionName(getString(R.string.activity_text_trans));
        button.setTransitionName(getString(R.string.activity_mixed_trans));

        Pair<View, String> pair1 = Pair.create(imageView, imageView.getTransitionName());
        Pair<View, String> pair2 = Pair.create(textView, textView.getTransitionName());
        Pair<View, String> pair3 = Pair.create(button, button.getTransitionName());
        ActivityOptionsCompat options = ActivityOptionsCompat.
                makeSceneTransitionAnimation(this, pair1, pair2, pair3);
        startActivity(intent, options.toBundle());
    }
    else {
        startActivity(intent);
    }
}