kotlin android - 使用构建器模式和 Java 8 lambda 的自定义对话框
kotlin android - Custom Dialog with Builder Pattern and Java 8 lambda
我有一个自定义对话框 class 定义了遵循构建器模式。我的代码没有问题。但现在我想重建以便能够在 java 8 lambda
上使用
CustomDialogList.kt
class CustomDialogList(context: Context,
private var title: Int?,
private var icon: Int?,
private var map: Map<String, Any>,
private var listner: OnItemClickListener) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.custom_dialog_list)
txtTitle.text = context.getString(title!!)
txtTitle.setCompoundDrawablesWithIntrinsicBounds(icon!!, 0, 0, 0)
val listString: MutableList<String> = mutableListOf()
val listObject: MutableList<Any> = mutableListOf()
for ((k, v) in map) {
listString.add(k)
listObject.add(v)
}
val adapter = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listString)
listView.adapter = adapter
listView.setOnItemClickListener { _, _, i, _ ->
listner.onClickResult(this, listObject[i], listString[i], i)
}
}
interface OnItemClickListener {
fun onClickResult(dialog: CustomDialogList, obj: Any?, text: String, position: Int)
}
class Builder(private var context: Context) {
private var listner: OnItemClickListener? = null
private var title: Int? = null
private var icon: Int? = null
private var map: Map<String, Any> = mapOf()
fun withTitle(title: Int): Builder {
this.title = title
return this
}
fun withIcon(icon: Int): Builder {
this.icon = icon
return this
}
fun withMap(map: Map<String, Any>): Builder {
this.map = map
return this
}
fun setOnItemClick(listner: OnItemClickListener): Builder {
this.listner = listner
return this
}
fun show() = CustomDialogList(context, title, icon, map, listner!!).show()
}
}
定义后的语法
CustomDialogList.Builder(this)
.withTitle(R.string.add)
.withIcon(R.drawable.ic_add)
.withMap(mapOf())
.setOnItemClick(object : CustomDialogList.OnItemClickListener {
override fun onClickResult(dialog: CustomDialogList, obj: Any?, text: String, position: Int) {
// do something
dialog.dismiss()
}
})
.show()
这是我要构建的示例
CustomDialogList.Builder(this)
.withTitle(R.string.add)
.withIcon(R.drawable.ic_add)
.withMap(mapOf())
.setOnItemClick({ dialog, obj, text, position ->
// do something
dialog.dismiss()
})
.show()
第一次post提问,希望大家见谅我的英文不好。并感谢任何人都可以帮助我 :)
KT-7770:SAM 不适用于 Kotlin 中定义的接口。
换句话说,lambda表达式
{ dialog, obj, text, position -> ... }
可以是Java函数式接口或Kotlin函数,但不能是Kotlin函数式接口。
如果你删除你的OnItemClickListener
界面并写
typealias OnItemClickListener = (CustomDialogList, Any?, String, Int) -> Unit
在顶层,或修改类型签名,您的第二个示例将起作用(而您的第一个将失败)。如果您改为在 Java 中定义接口,两者都可以工作。
我有一个自定义对话框 class 定义了遵循构建器模式。我的代码没有问题。但现在我想重建以便能够在 java 8 lambda
上使用CustomDialogList.kt
class CustomDialogList(context: Context,
private var title: Int?,
private var icon: Int?,
private var map: Map<String, Any>,
private var listner: OnItemClickListener) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.custom_dialog_list)
txtTitle.text = context.getString(title!!)
txtTitle.setCompoundDrawablesWithIntrinsicBounds(icon!!, 0, 0, 0)
val listString: MutableList<String> = mutableListOf()
val listObject: MutableList<Any> = mutableListOf()
for ((k, v) in map) {
listString.add(k)
listObject.add(v)
}
val adapter = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listString)
listView.adapter = adapter
listView.setOnItemClickListener { _, _, i, _ ->
listner.onClickResult(this, listObject[i], listString[i], i)
}
}
interface OnItemClickListener {
fun onClickResult(dialog: CustomDialogList, obj: Any?, text: String, position: Int)
}
class Builder(private var context: Context) {
private var listner: OnItemClickListener? = null
private var title: Int? = null
private var icon: Int? = null
private var map: Map<String, Any> = mapOf()
fun withTitle(title: Int): Builder {
this.title = title
return this
}
fun withIcon(icon: Int): Builder {
this.icon = icon
return this
}
fun withMap(map: Map<String, Any>): Builder {
this.map = map
return this
}
fun setOnItemClick(listner: OnItemClickListener): Builder {
this.listner = listner
return this
}
fun show() = CustomDialogList(context, title, icon, map, listner!!).show()
}
}
定义后的语法
CustomDialogList.Builder(this)
.withTitle(R.string.add)
.withIcon(R.drawable.ic_add)
.withMap(mapOf())
.setOnItemClick(object : CustomDialogList.OnItemClickListener {
override fun onClickResult(dialog: CustomDialogList, obj: Any?, text: String, position: Int) {
// do something
dialog.dismiss()
}
})
.show()
这是我要构建的示例
CustomDialogList.Builder(this)
.withTitle(R.string.add)
.withIcon(R.drawable.ic_add)
.withMap(mapOf())
.setOnItemClick({ dialog, obj, text, position ->
// do something
dialog.dismiss()
})
.show()
第一次post提问,希望大家见谅我的英文不好。并感谢任何人都可以帮助我 :)
KT-7770:SAM 不适用于 Kotlin 中定义的接口。
换句话说,lambda表达式
{ dialog, obj, text, position -> ... }
可以是Java函数式接口或Kotlin函数,但不能是Kotlin函数式接口。
如果你删除你的OnItemClickListener
界面并写
typealias OnItemClickListener = (CustomDialogList, Any?, String, Int) -> Unit
在顶层,或修改类型签名,您的第二个示例将起作用(而您的第一个将失败)。如果您改为在 Java 中定义接口,两者都可以工作。