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
package com.example.smart.ui.query.view
 
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.view.isVisible
import com.example.smart.R
import com.example.smart.databinding.ItemQueryTagBinding
import com.example.smart.ui.query.bean.QueryTagBean
import com.example.smart.ui.query.event.ITagEvent
 
 
class QueryTagView @JvmOverloads constructor(
    context: Context,
    private val tagData: QueryTagBean,
    val showIcon: Boolean = false,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : FrameLayout(context, attrs, defStyleAttr) {
 
    private var tagEvent: ITagEvent? = null
 
    companion object {
        fun create(context: Context, tagData: QueryTagBean, showIcon: Boolean = false) =
            QueryTagView(context, tagData, showIcon)
    }
 
    fun setTagEvent(tagEvent: ITagEvent) {
        this.tagEvent = tagEvent
    }
 
 
    private val binding by lazy(LazyThreadSafetyMode.NONE) {
        ItemQueryTagBinding.bind(this)
    }
 
 
    init {
        LayoutInflater.from(context).inflate(R.layout.item_query_tag, this)
 
        binding.run {
            tagNameTv.text = tagData.name
 
            //showIcon表示是否是已选tag
            tagNameTv.setOnClickListener {
                if (!showIcon) {
                    tagEvent?.onAdd(tagData)
                } else{
                    tagEvent?.onRemove(tagData)
                }
 
            }
 
            deleteImg.isVisible = showIcon
 
            deleteImg.setOnClickListener {
                if (showIcon) {
                    tagEvent?.onRemove(tagData)
                }
            }
        }
    }
 
 
    fun showIcon() = showIcon
 
 
}