Android DialogFragment 从下往上进入、从上往下退出实现

6 篇文章 0 订阅

实现此效果,直接上代码:

public class CustomFragment extends DialogFragment {

    
    @Bind(R.id.close)
    ImageView close;
    @Bind(R.id.confirm)
    Button confirm;
   

    private SomethingItem item;
    private SomethingItem tmpItem;
    private boolean flag;


    public static CustomFragment newInstance(DishTradeItem item, boolean flag) {
        CustomFragment f = new CustomFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("item", item);
        bundle.putBoolean("flag", flag);
        f.setArguments(bundle);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		//样式设置
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_fragment);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//默认不弹出软键盘
        getDialog().getWindow().setGravity(Gravity.BOTTOM);//Dialog处于页面的底部
        getDialog().setCanceledOnTouchOutside(true);//点击Dialog外围可以消除Dialog


        Bundle bundle = getArguments();
        if (bundle != null) {
            item = (SomethingItem) bundle.getSerializable("item");
            flag = bundle.getBoolean("flag",false);
            Gson gson = new Gson();
            /*深拷贝一份,点击确定后才变化*/
            tmpItem = gson.fromJson(gson.toJson(item), SomethingItem.class);
        }
        View root = inflater.inflate(R.layout.custom_layout, container, false);
        ButterKnife.bind(this, root);
        /**
		 *添加页面的初始化
		 **/
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        int maxHeight = (int) (metric.heightPixels * 0.8);//暂定高度
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, maxHeight);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }





    @OnClick(R.id.close)
    public void closeDialog() {
        dismissAllowingStateLoss();
    }


    @OnClick(R.id.confirm)
    public void confirm() {
        /**
		 *添加确认处理逻辑
		 **/
        dismissAllowingStateLoss();
    }

    


    @Override
    public void show(FragmentManager manager, String tag) {
		//当正在显示时,不再新弹
        if (manager.findFragmentByTag(tag) == null) {
            super.show(manager, tag);
        }
    }


}

对应的style如下:

    <style name="dialog_fragment_animation">
        <item name="android:windowEnterAnimation">@anim/dialog_in_bottom</item>
        <item name="android:windowExitAnimation">@anim/dialog_out_bottom</item>
    </style>

    <style name="dialog_fragment" parent="@android:style/Theme">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:padding">0dp</item>
        <item name="android:gravity">bottom</item>
        <item name="android:windowSoftInputMode">stateAlwaysHidden|adjustResize</item>
        <item name="android:windowAnimationStyle">@style/dialog_fragment_animation</item>
    </style>


动画文件:

dialog_in_bottom.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYDelta="100%p" android:toYDelta="0%p"
        android:duration="600"/>

</set>
dialog_out_bottom.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYDelta="100%p" android:toYDelta="0%p"
        android:duration="600"/>

</set>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值