设置Android片段进入和退出转换?

时间:2015-02-11 08:53:50

标签: android android-fragments

我正在尝试为DialogFragment设置进入和退出转换,这似乎很直接。以下是我的DrinkDetailActivity中创建对话框的代码:

    fab.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            //create and display the dialog
            DialogFragment orderFragment = new OrderDrinkDialogFragment();

            //set the slide transition for the fragment
            Transition dialSlide = new Slide();
            dialSlide.setDuration(300);

            orderFragment.setEnterTransition(dialSlide);
            orderFragment.setExitTransition(dialSlide);
            orderFragment.show(getFragmentManager(), "order");
        }
    });

以下是我的OrderDrinkDialogFragment类的代码:

public class OrderDrinkDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(DrinkDetailActivity.activity);

        //set up the Linear Layout of the dialog
        LinearLayout layout = new LinearLayout(DrinkDetailActivity.activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                                     LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(params);

        //set up the EditText view
        EditText nameField = new EditText(DrinkDetailActivity.activity);
        nameField.setHint(R.string.dialog_name_hint);

        //add the EditText view to the layout
        layout.addView(nameField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                            LinearLayout.LayoutParams.WRAP_CONTENT));

        //set layout as the view of the dialog
        builder.setView(layout);

        return builder.create();
    }
}

DrinkDetailActivity.activity只是来自this实例中的DrinkDetailActivity。出于某种原因,当对象show()或它被解除时,对话框不会转换,它只是弹出和退出视图。 This教程帮助我了解了应用程序其余部分的Activity转换,并按照相同的说明进行了片段转换。我做错了什么?

3 个答案:

答案 0 :(得分:10)

这对我有用。我向上滑动动画

<?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:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        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:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p" />

</set>
你的风格中的

添加这个

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
</style>

现在终于在你的onCreateDialog() Dialog片段中添加了这个

public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    return dialog;
}

答案 1 :(得分:0)

您需要编写一种样式,以便在Dialog

等任何窗口上显示动画
<style name="AnimDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/fadein</item>
    <item name="android:windowExitAnimation">@anim/fadeout</item>
</style>

对于您的对话框,您需要实例化

Dialog animatedDialog = new Dialog(MainActivity.this,R.style.AnimDialog);

答案 2 :(得分:-1)

DialogFragment不仅是一个对话框,还有一个片段,有关如何使用它的详细信息,请参阅Basic Dialog。您在Fragment中使用了onCreateDialog,这可能是导致问题的原因。因此,使用onCreateView创建片段对话框视图。

希望这个答案可以帮到你。