Android 封装一个通用DialogFragment


/**
 * Dialog通用样式
 */
open abstract class BaseDialog : DialogFragment(), DialogAnimStyle {
    @LayoutRes
    protected var mLayoutResId = 0
    private var mDimAmount = 0.5f //背景昏暗度
    private var mShowBottomEnable //是否底部显示
            = false
    private var mMargin = 0 //左右边距
    private var mAnimStyle = 0 //进入退出动画
    private var mOutCancel = true //点击外部取消
    private var mWidth = 0
    private var mHeight = 0
    //父类activity
    lateinit var mActivity: AppCompatActivity
    lateinit var mContext: Context
    /** 根布局  */
    lateinit var mRootView: View

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
        mActivity=context as AppCompatActivity
    }

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen)
        mLayoutResId = setUpLayoutId()
    }

    @Nullable
    override fun onCreateView(
        inflater: LayoutInflater,
        @Nullable container: ViewGroup?,
        @Nullable savedInstanceState: Bundle?
    ): View {
        mRootView= inflater.inflate(mLayoutResId, container, false)
        convertView(BaseDialogViewHolder(mRootView), this)
        return mRootView
    }

    override fun onStart() {
        super.onStart()
        initParams()
    }

    private fun initParams() {
        val window: Window? = dialog?.window
        if (window != null) {
            val params = window.attributes
            params.dimAmount = mDimAmount

            //设置dialog显示位置
            if (mShowBottomEnable) {
                params.gravity = Gravity.BOTTOM
            }

            //设置dialog宽度
            if (mWidth == 0) {
                params.width =
                    getScreenWidth(mContext) - 2 * dp2px(mContext, mMargin.toFloat())
            } else {
                params.width = dp2px(mContext, mWidth.toFloat())
            }

            //设置dialog高度
            if (mHeight == 0) {
                params.height = WindowManager.LayoutParams.WRAP_CONTENT
            } else {
                params.height = dp2px(mContext, mHeight.toFloat())
            }

            //设置dialog动画
            if (mAnimStyle != 0) {
                window.setWindowAnimations(mAnimStyle)
            }
            window.attributes = params
        }
        isCancelable = mOutCancel
    }

    /**
     * 设置背景昏暗度
     * @param dimAmount 0 - 1f
     * @return
     */
    fun setDimAmout(@FloatRange(from = 0.toDouble(), to = 1.toDouble()) dimAmount: Float): BaseDialog {
        mDimAmount = dimAmount
        return this
    }

    /**
     * 是否显示底部
     *
     * @param showBottom
     * @return
     */
    fun setShowBottom(showBottom: Boolean): BaseDialog {
        mShowBottomEnable = showBottom
        return this
    }

    /**
     * 设置宽高
     * @param width
     * @param height
     * @return
     */
    fun setSize(width: Int, height: Int): BaseDialog {
        mWidth = width
        mHeight = height
        return this
    }

    /**
     * 设置左右margin
     *
     * @param margin
     * @return
     */
    fun setMargin(margin: Int): BaseDialog {
        mMargin = margin
        return this
    }

    /**
     * 设置进入退出动画
     *
     * @param animStyle
     * @return
     */
    fun setAnimStyle(@StyleRes animStyle: Int): BaseDialog {
        mAnimStyle = animStyle
        return this
    }

    /**
     * 顶部
     * @return BaseDialog
     */
    fun setTopAnimStylent(): BaseDialog {
        mAnimStyle = TOP
        return this
    }

    /**
     * 中心
     * @return BaseDialog
     */
    fun setCenterAnimStylent(): BaseDialog {
        mAnimStyle = CENTER
        return this
    }

    /**
     * 底部
     * @return BaseDialog
     */
    fun setBottomAnimStylent(): BaseDialog {
        mAnimStyle = BOTTOM
        return this
    }

    /**
     * 设置是否点击外部取消
     * @param outCancel
     * @return
     */
    fun setOutCancel(outCancel: Boolean): BaseDialog {
        mOutCancel = outCancel
        return this
    }

    fun show(manager: FragmentManager): BaseDialog {
        super.show(manager, System.currentTimeMillis().toString())
        return this
    }

    /**
     * 设置dialog布局
     *
     * @return
     */
    abstract fun setUpLayoutId(): Int

    /**
     * 操作dialog布局
     * @param holder
     * @param dialog
     */
    abstract fun convertView(holder: BaseDialogViewHolder, dialog: BaseDialog)

    companion object {
        /**
         * 获取屏幕宽度
         * @param context
         * @return
         */
        fun getScreenWidth(context: Context): Int {
            val displayMetrics: DisplayMetrics = context.resources.displayMetrics
            return displayMetrics.widthPixels
        }

        fun dp2px(context: Context, dipValue: Float): Int {
            val scale: Float = context.resources.displayMetrics.density
            return (dipValue * scale + 0.5f).toInt()
        }
    }
}

dialog弹出的动画


interface DialogAnimStyle {
    /**
     * 顶部
     */
    val TOP: Int
        get() = R.style.TopAnimStyle

    /**
     * 中心
     */
    val CENTER: Int
        get() = R.style.centerAnimStyle

    /**
     * 底部
     */
    val BOTTOM: Int
        get() = R.style.BottomAnimStyle

}

通用的dialog

/**
 * 公用样式Dialog
 */
class CommonDialog : BaseDialog() {
    companion object {
        fun newInstance(): CommonDialog {
            return CommonDialog()
        }
    }

    private var convertListener: ViewConvertListener? = null

    /**
     * 设置Dialog布局
     * @param layoutId
     * @return
     */
    fun setLayoutId(@LayoutRes layoutId: Int): CommonDialog {
        mLayoutResId = layoutId
        return this
    }

    override fun setUpLayoutId(): Int {
        return mLayoutResId
    }

    override fun convertView(holder: BaseDialogViewHolder, dialog: BaseDialog) {
        if (holder==null){
            LogUtils.i("CommonDialog::convertView::holder:为空")
            return
        }
        if (dialog==null){
            LogUtils.i("CommonDialog::convertView::dialog:为空")
            return
        }
         convertListener?.convertView(holder, dialog)
    }

    fun setConvertListener(convertListener: ViewConvertListener?): CommonDialog {
        this.convertListener = convertListener
        return this
    }
}

BaseDialogViewHolder


class BaseDialogViewHolder( val convertView: View) {
    private val views: SparseArray = SparseArray()

    /**
     * 获取View
     * @param viewId
     * @param 
     * @return
     */
    fun  getView(@IdRes viewId: Int): T? {
        var view = views[viewId]
        if (view == null) {
            view = convertView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T?
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     */
    fun setText(viewId: Int, text: String?) {
        val textView = getView(viewId)!!
        textView.text = text
    }

    /**
     * 设置字体颜色
     *
     * @param viewId
     * @param colorId
     */
    fun setTextColor(viewId: Int, colorId: Int) {
        val textView = getView(viewId)!!
        textView.setTextColor(colorId)
    }

    /**
     * 设置背景图片
     *
     * @param viewId
     * @param resId
     */
    fun setBackgroundResource(viewId: Int, resId: Int) {
        val view = getView(viewId)!!
        view.setBackgroundResource(resId)
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param colorId
     */
    fun setBackgroundColor(viewId: Int, colorId: Int) {
        val view = getView(viewId)!!
        view.setBackgroundColor(colorId)
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param listener
     */
    fun setOnClickListener(viewId: Int, listener: View.OnClickListener?) {
        val view = getView(viewId)!!
        view.setOnClickListener(listener)
    }
}

你可能感兴趣的:(Android 封装一个通用DialogFragment)