美文网首页
android DialogFragment 的各种用法

android DialogFragment 的各种用法

作者: xq9527 | 来源:发表于2022-11-10 15:12 被阅读0次

    前言:

    各位同学大家好 有段时间没有给各位更新文章了,最近在写新的项目 之前的手游sdk 都是用透明的activity 效果有缺陷,现在我改成用这个dialogfragment 来实现 , 废话不多说我们正式开始

    效果图

    image.png
    image.png

    看到效果图 有些同学说可以用dialog和popupwindow 或者是透明的activtiy

    具体实现

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/activityTextWhite"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15.5dp"
                android:gravity="center_horizontal"
                android:text="请选择乘车人数"
                android:textColor="#666666"
                android:textSize="16sp" />
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16.5dp"
                android:gravity="center_horizontal"
                android:text="1人"
                android:textColor="#333333"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16.5dp"
                android:gravity="center_horizontal"
                android:text="2人"
                android:textColor="#333333"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/tv3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16.5dp"
                android:gravity="center_horizontal"
                android:text="3人"
                android:textColor="#333333"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/tv4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16.5dp"
                android:layout_marginBottom="16.5dp"
                android:gravity="center_horizontal"
                android:text="4人"
                android:textColor="#333333"
                android:textSize="18sp" />
        </LinearLayout>
    
    
        <Button
            android:id="@+id/btn_cancel"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginBottom="30dp"
            android:background="@color/activityTextWhite"
            android:text="取消" />
    </LinearLayout>
    
    

    布局效果

    image.png

    Java代码逻辑

    • 加载布局

        @SuppressLint("InflateParams")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            //加这句话去掉自带的标题栏
            Objects.requireNonNull(getDialog()).requestWindowFeature(Window.FEATURE_NO_TITLE);
            mRootView = inflater.inflate(R.layout.fragment_nice_dialog, null);
            //从下到上的动画
            AnimationUtil.slideToUp(mRootView);
            return mRootView;
        }
    
    

    这里的用法和fragment 很像我们用布局填充器 将布局填充到我们的dialogfragment 里面来

    • 设置限制位置和以及动画

        @Override
        public void onStart() {
            super.onStart();
            Window window = Objects.requireNonNull(getDialog()).getWindow();
            WindowManager.LayoutParams params = Objects.requireNonNull(window).getAttributes();
            //设置显示在底部
            params.gravity = Gravity.CENTER;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
            View decorView = window.getDecorView();
            decorView.setPadding(100, 100, 100, 0);
            decorView.setBackground(new ColorDrawable(Color.TRANSPARENT));
            //设置点击空白处关闭,也能启动从上到下的动画
            decorView.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        slideDown();
                    }
                    return true;
                }
            });
        }
      // 点击空白部分可以关闭 
        private void slideDown() {
            AnimationUtil.slideToDown(mRootView, new AnimationUtil.AnimationEndListener() {
                @Override
                public void onFinish() {
                    dismiss();
                }
            });
        }
    

    具体调用显示

    
         final NiceDialogFragment niceDialogFragment = new NiceDialogFragment(this);
        niceDialogFragment.show(getSupportFragmentManager(), "android");
    
    

    上面是简单使用这个dialogfragment 我们在开发手游sdk的时候 我们需要稍微封装一下 我们的代码

    Basedialogfragment

    package com.xcynice.testdialogfragment;
    import android.annotation.SuppressLint;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.DialogFragment;
    import java.util.Objects;
    /**
     * 
     * 创建人:xuqing
     * 创建时间:2022年11月11日14:54:15
     * 类说明:base基类来处理dialogfragment
     * 
     * 
     */
    public  abstract  class BaseDialogFragment  extends DialogFragment implements View.OnClickListener {
    
        private String  layout;
        private View mRootView;
       //通过构造方法传入我们的布局文件
        public BaseDialogFragment(String layout) {
            this.layout = layout;
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
       //这里用反射来获取布局文件
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Objects.requireNonNull(getDialog()).requestWindowFeature(Window.FEATURE_NO_TITLE);
            mRootView = inflater.inflate(ResourceUtil.getLayoutId(getContext(),layout), null);
            //从下到上的动画
            //AnimationUtil.slideToUp(mRootView);
            gameGetExtraParams();
            findViewById();
            gameSetListener();
            return mRootView;
        }
    
      //定义findViewById方法来替换掉系统的用反射获取  
        public View findViewById(String viewId) {
            return mRootView.findViewById(ResourceUtil.getId(getContext(), viewId));
        }
        protected abstract void findViewById();
    
        protected abstract void gameSetListener();
    
        protected abstract void gameGetExtraParams();
    
        @Override
        public void onStart() {
            super.onStart();
            Window window = Objects.requireNonNull(getDialog()).getWindow();
            WindowManager.LayoutParams params = Objects.requireNonNull(window).getAttributes();
            //设置显示在底部
            params.gravity = Gravity.CENTER;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
            View decorView = window.getDecorView();
            decorView.setPadding(100, 100, 100, 0);
            decorView.setBackground(new ColorDrawable(Color.TRANSPARENT));
            //设置点击空白处关闭,也能启动从上到下的动画
            decorView.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        slideDown();
                    }
                    return true;
                }
            });
        }
        private void slideDown() {
            AnimationUtil.slideToDown(mRootView, new AnimationUtil.AnimationEndListener() {
                @Override
                public void onFinish() {
                    dismiss();
                }
            });
        }
    }
    

    具体业务逻辑的

    • 我们的ExitDialogFragment
    package com.xcynice.testdialogfragment;
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    /**
     *
     * 创建人:xuqing
     * 创建时间 :2022年11月11日15:08:04
     * 类说明 :退出弹窗
     *
     *
     */
    
    public  class ExitDialogFragment extends BaseDialogFragment {
        private Button exit_confirm_btn,exit_cancel_btn;
        private Activity context;
        public ExitDialogFragment(Activity context,String layout) {
            super(layout);
            this.context=context;
        }
    
        @Override
        protected void findViewById() {
            exit_confirm_btn= (Button) findViewById(GR.id.exit_confirm_btn);
            exit_cancel_btn= (Button) findViewById(GR.id.exit_cancel_btn);
        }
    
        @Override
        protected void gameSetListener() {
            exit_confirm_btn.setOnClickListener(this);
            exit_cancel_btn.setOnClickListener(this);
    
        }
    
        @Override
        protected void gameGetExtraParams() {
    
        }
    
        @Override
        public void onClick(View v) {
           if(v.getId()==ResourceUtil.getId(context,GR.id.exit_confirm_btn)){
               try {
                   context.finish();
                   System.exit(0);
                   android.os.Process.killProcess(android.os.Process.myPid());
               }catch (Exception ex) {
                   Log.i("error",ex.getMessage());
               }
           }else if(v.getId()==ResourceUtil.getId(context,GR.id.exit_cancel_btn)){
               dismiss();
           }
    
        }
    }
    
    

    经过我们稍微的封装 我们退出弹窗可以简化很多.我们只需要在构造方法里面传入我们布局文件的名字即可 然后在findViewById方法里面调用父类的findViewById方法来初始化控件即可

    最后总结:

    我们使用了 DialogFragment 可以看到比起dialog更为强大结合了fragment和dialog的特性。是非常使用用来做手游sdk开发的 , 最后希望我都文章能帮助各位同学工作和学习 。如果觉得文章还不错希望能给我一个star 和转发

    相关文章

      网友评论

          本文标题:android DialogFragment 的各种用法

          本文链接:https://www.haomeiwen.com/subject/epwztdtx.html