|
|
@@ -0,0 +1,97 @@
|
|
|
+package com.atmob.keyboard_android.ui.popup.base
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.graphics.Color
|
|
|
+import android.graphics.drawable.ColorDrawable
|
|
|
+import android.view.Gravity
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.view.Window
|
|
|
+import android.view.WindowManager
|
|
|
+import android.widget.PopupWindow
|
|
|
+import com.atmob.keyboard_android.ui.core.LayoutCallback
|
|
|
+
|
|
|
+/**
|
|
|
+ * 使用PopupWindow来实现弹窗
|
|
|
+ */
|
|
|
+abstract class BasePopupDialog(context: Context, private val hostWindow: Window) :
|
|
|
+ LayoutCallback {
|
|
|
+ private var mPopupWindow: PopupWindow? = null
|
|
|
+
|
|
|
+ init {
|
|
|
+ onLayoutBefore()
|
|
|
+
|
|
|
+ // 填充布局
|
|
|
+ val contentView = LayoutInflater.from(context).inflate(onInflaterViewId(), null, false)
|
|
|
+
|
|
|
+ // 配置 PopupWindow
|
|
|
+ mPopupWindow = PopupWindow(
|
|
|
+ contentView,
|
|
|
+ WindowManager.LayoutParams.MATCH_PARENT,
|
|
|
+ WindowManager.LayoutParams.MATCH_PARENT
|
|
|
+ ).apply {
|
|
|
+ // 不获取焦点,否则获取焦点会将软键盘关闭
|
|
|
+ isFocusable = false
|
|
|
+ // 保持输入法活跃
|
|
|
+ inputMethodMode = PopupWindow.INPUT_METHOD_NEEDED
|
|
|
+ // 点击外部区域,是否关闭弹窗
|
|
|
+ isOutsideTouchable = isCanOutsideTouchable()
|
|
|
+ // 设置背景,否则还是会关闭
|
|
|
+ setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
|
|
+ }
|
|
|
+
|
|
|
+ onInflaterViewAfter(contentView)
|
|
|
+ onBindView(contentView)
|
|
|
+ setData()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 显示Loading
|
|
|
+ */
|
|
|
+ fun show() {
|
|
|
+ if (mPopupWindow?.isShowing == true) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 显示在屏幕中央
|
|
|
+ mPopupWindow?.showAtLocation(
|
|
|
+ hostWindow.decorView,
|
|
|
+ Gravity.CENTER,
|
|
|
+ 0,
|
|
|
+ 0
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onLayoutBefore() {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onInflaterViewAfter(view: View) {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onBindView(view: View) {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun setData() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否允许点击外部区域,关闭弹窗
|
|
|
+ */
|
|
|
+ protected open fun isCanOutsideTouchable(): Boolean {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否正在显示
|
|
|
+ */
|
|
|
+ fun isShowing(): Boolean {
|
|
|
+ return mPopupWindow?.isShowing == true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 隐藏Loading
|
|
|
+ */
|
|
|
+ fun dismiss() {
|
|
|
+ mPopupWindow?.dismiss()
|
|
|
+ }
|
|
|
+}
|