我从下面的片段中运行GirlQualitiesChoiceDialog:
girlType.setOnClickListener {
GirlQualitiesChoiceDialog.show(activity, InitialDataSet.instance.QUALITY_TYPE_COLOR){
Picasso.with(context).load(it.resId).into(colorTypeImage as ImageView)
//GirlQualitiesChoiceDialog.this.dismiss() - need to dismiss the created dialog
}
}
指定监听器的位置。我需要的是在加载图片后关闭对话框。 GirlQualitiesChoiceDialog.this.dismiss()无效。
有人可以告诉我,我怎么能在这里解雇GirlQualitiesChoiceDialog?假设我需要以某种方式将对话回调传递给lambda,但是无法实现。
以下是我的对话框的代码:
class GirlQualitiesChoiceDialog : DialogFragment() {
companion object {
val ARG_QUALITY_TYPE = "ARG_QUALITY_TYPE"
private lateinit var itemClickInterface: (Quality) -> Unit
fun show(activity: Activity, qualityType: Int, itemClick: (Quality) -> Unit) {
itemClickInterface = itemClick
GirlQualitiesChoiceDialog().apply {
arguments = Bundle().apply {
putInt(ARG_QUALITY_TYPE, qualityType)
}
}.show((activity as AppCompatActivity).supportFragmentManager, TAG)
}
}
private lateinit var adapter: QualitiesAdapter
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val choiceDialogView = activity.getLayoutInflater().inflate(horizontal_list_view, null)
val layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
choiceDialogView.horisontalCardsRecyclerView.setLayoutManager(layoutManager)
with(ItemClickSupport.addTo(choiceDialogView.horisontalCardsRecyclerView)) {
setOnItemLongClickListener { recyclerView, position, v ->
val quality = v.getTag() as Quality;
dismiss()
true
}
}
var qualityType = arguments.getInt(ARG_QUALITY_TYPE)
var title = -1
when (qualityType) {
InitialDataSet.instance.QUALITY_TYPE_COLOR -> {
title = R.string.choose_color_type
adapter = QualitiesAdapter(InitialDataSet.instance.qualities, itemClickInterface)
}
}
choiceDialogView.horisontalCardsRecyclerView.adapter = adapter
return AlertDialog.Builder(activity)
.setTitle(title)
.setView(choiceDialogView)
.create()
}
}
和适配器
class QualitiesAdapter(val qualities: List<Quality>, val itemClick: (Quality) -> Unit) :
RecyclerView.Adapter<QualitiesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.selection_card, parent, false)
return ViewHolder(view, itemClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindForecast(qualities[position])
}
override fun getItemCount() = qualities.size
class ViewHolder(view: View, val itemClick: (Quality) -> Unit) : RecyclerView.ViewHolder(view) {
fun bindForecast(quality: Quality) {
with(quality) {
Picasso.with(itemView.context).load(resId).into(itemView.selectionImage)
(itemView as BaseCardView).setStatusLabel(desc)
itemView.setTag(quality)
itemView.setOnClickListener { itemClick(this) }
}
}
}
答案 0 :(得分:0)
我建议您从show方法返回对话框的实例,然后您可以在回调中将其关闭。
在GirlQualitiesChoiceDialog中更改显示如下内容:
fun show(activity: Activity, qualityType: Int, itemClick: (Quality) -> Unit): GirlQualitiesChoiceDialog {
itemClickInterface = itemClick
val dialog = GirlQualitiesChoiceDialog().apply {
arguments = Bundle().apply {
putInt(ARG_QUALITY_TYPE, qualityType)
}
}
dialog.show((activity as AppCompatActivity).supportFragmentManager, TAG)
return dialog
}
这是听众代码:
girlType.setOnClickListener {
val dialog = GirlQualitiesChoiceDialog.show(activity, InitialDataSet.instance.QUALITY_TYPE_COLOR) {
Picasso.with(context).load(it.resId).into(colorTypeImage as ImageView)
dialog.dismiss()
}
}