18034727885
2026-05-14 dad2ded3e9e9d2edf55c7ce10e791588b526961f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)
        }
 
    }
}