package com.example.smart.widget.dialog.list
|
|
import android.annotation.SuppressLint
|
import android.content.Context
|
import android.os.Bundle
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import android.widget.TextView
|
import androidx.fragment.app.DialogFragment
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.RecyclerView
|
import com.example.basecore.widget.CheckBoxSample
|
import com.example.smart.R
|
|
/**
|
* @author: dai hang
|
* @data: 2024/6/17 11:11
|
* @description:
|
*/
|
class CommListDialog(val datas: List<CommonListBean>, val listener : ItemClickListener) : DialogFragment() {
|
|
override fun onStart() {
|
super.onStart()
|
val dialog = dialog
|
if (dialog != null) {
|
try {
|
val display = requireActivity().windowManager.defaultDisplay
|
val layoutParams = dialog.window!!.attributes
|
layoutParams.width = (display.width * 0.9).toInt()
|
dialog.window!!.attributes = layoutParams
|
} catch (e: Exception) {
|
}
|
}
|
|
}
|
|
|
@SuppressLint("MissingInflatedId")
|
override fun onCreateView(
|
inflater: LayoutInflater,
|
container: ViewGroup?,
|
savedInstanceState: Bundle?
|
): View {
|
val mView: View =
|
inflater.inflate(R.layout.dialog_list_common, container, false)
|
val recyclerView = mView.findViewById<RecyclerView>(R.id.recycler_view)
|
|
|
recyclerView.layoutManager =
|
LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
|
recyclerView.adapter = ListAdapter(requireActivity(), datas)
|
|
|
mView.findViewById<TextView>(R.id.confirm_tv)?.setOnClickListener(View.OnClickListener {
|
dismiss()
|
|
})
|
mView.findViewById<TextView>(R.id.cancel_tv)?.setOnClickListener(View.OnClickListener {
|
dismiss()
|
})
|
|
|
|
|
return mView
|
}
|
|
interface ItemClickListener {
|
fun onItemClick(bean: CommonListBean)
|
}
|
|
inner class ListAdapter(val mContext: Context, val list: List<CommonListBean>) :
|
RecyclerView.Adapter<PopupViewHolder>() {
|
|
override fun onBindViewHolder(holder: PopupViewHolder, position: Int) {
|
val bean = list[position]
|
holder.contentTv.text = bean.name
|
|
holder.checkbox.setChecked(bean.checked, false)
|
holder.itemView.setOnClickListener {
|
if (!bean.checked) {
|
holder.checkbox.setChecked(true, false)
|
} else {
|
holder.checkbox.setChecked(false, false)
|
}
|
bean.checked = !bean.checked
|
listener.onItemClick(bean)
|
}
|
}
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PopupViewHolder {
|
|
return PopupViewHolder(
|
LayoutInflater.from(mContext).inflate(R.layout.common_list_dialog_item, parent, false)
|
)
|
}
|
|
|
override fun getItemCount() = list.size
|
|
}
|
|
|
inner class PopupViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
|
val contentTv: TextView
|
val checkbox: CheckBoxSample
|
|
init {
|
contentTv = itemView.findViewById(R.id.content_tv)
|
checkbox = itemView.findViewById(R.id.checkbox)
|
}
|
|
}
|
}
|