Fetch the repository succeeded.
package com.zw.yzk.component.common.dialog
import android.content.Context
import android.graphics.Point
import android.widget.TextView
import android.support.v7.app.AlertDialog
import android.view.*
/**
* 示例用法
* DialogBuilder.Builder(this, R.layout.dialog_select_image)
* .setAnimation(R.style.bottom_animation)
* .addItem(DialogBuilder.Item(R.id.album, getString(R.string.album), View.OnClickListener {
* getCameraPermission()
* }))
* .addItem(DialogBuilder.Item(R.id.cancel, getString(R.string.cancel)))
* .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
* .setGravity(Gravity.BOTTOM)
* .setCancelable(true)
* .build().show()
*/
class DialogBuilder {
class Item(id: Int, text: String? = null, listener: View.OnClickListener? = null) {
val itemId: Int = id
val itemText: String? = text
val clickListener: View.OnClickListener? = listener
}
//对话框属性设置
private class AlertParams(var context: Context, var view: View) {
val itemList: MutableList<Item> = mutableListOf()
//是否可点击区域外取消
var cancelable: Boolean = false
//动画
var animationId: Int = 0
//显示位置
var gravity: Int = Gravity.NO_GRAVITY
//设置对话框style
var style: Int = 0
//设置大小
var size: Point? = null
}
class Builder(context: Context, view: View) {
private val params: AlertParams = AlertParams(context, view)
constructor(context: Context, layoutId: Int) : this(context, LayoutInflater.from(context).inflate(layoutId, null))
fun addItem(item: Item): Builder {
params.itemList.add(item)
return this
}
fun setCancelable(cancelable: Boolean): Builder {
params.cancelable = cancelable
return this
}
fun setAnimation(animation: Int): Builder {
params.animationId = animation
return this
}
fun setGravity(gravity: Int): Builder {
params.gravity = gravity
return this
}
fun setStyle(style: Int): Builder {
params.style = style
return this
}
fun setSize(width: Int, height: Int): Builder {
params.size = Point(width, height)
return this
}
fun build(): AlertDialog {
val dialog = AlertDialog.Builder(params.context, params.style)
.setView(params.view).setCancelable(params.cancelable).create()
//设置背景透明
val window = dialog.window
window?.let {
window.setBackgroundDrawableResource(android.R.color.transparent)
window.decorView.setPadding(0, 0, 0, 0)
window.attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT
window.attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
params.size?.let {
window.attributes.width = params.size!!.x
window.attributes.height = params.size!!.y
}
//设置展示位置
window.attributes.gravity = params.gravity
//设置动画
window.setWindowAnimations(params.animationId)
}
//设置item
params.itemList.forEach { item ->
params.view.findViewById<View>(item.itemId)?.apply {
setOnClickListener {
item.clickListener?.onClick(it)
dialog.dismiss()
}
item.itemText?.let {
(this as TextView).text = item.itemText
}
}
}
return dialog
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。