18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
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
118
119
120
121
package com.example.smart.ui.query.view
 
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.view.isVisible
import com.example.basecore.util.dp2px
import com.example.smart.R
import com.example.smart.databinding.QueryTagLayoutBinding
import com.example.smart.ui.query.bean.QueryBean
import com.example.smart.ui.query.bean.QueryInfoBean
import com.example.smart.ui.query.bean.QueryTagBean
import com.example.smart.ui.query.event.ITagEvent
import com.google.android.flexbox.FlexboxLayout
 
 
class QueryTagContainer @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr) {
 
    private val queryList = mutableListOf<QueryTagBean>()
 
    private  var selectLayout: QueryTagLayout
 
    private  var selectFlexBox: FlexboxLayout
 
    private var mITagEvent: ITagEvent = MyTagEvent()
 
 
    init {
        setPadding(dp2px(8), 0, dp2px(8), 0)
 
        orientation = VERTICAL
        selectLayout = generateLayout(QueryInfoBean("已选", ArrayList()))
        selectLayout.isVisible = false
        selectFlexBox = selectLayout.getFlexView()
        addView(selectLayout)
    }
 
 
    fun buildTagLayout(queryBean: QueryBean) {
        if (queryBean.planType != null && queryBean.planType.list.isNotEmpty()) {
            addView(generateLayout(queryBean.planType))
        }
 
        if (queryBean.planState != null && queryBean.planState.list.isNotEmpty()) {
            addView(generateLayout(queryBean.planState))
        }
 
        if (queryBean.client != null && queryBean.client.list.isNotEmpty()) {
            addView(generateLayout(queryBean.client))
        }
    }
 
    private fun generateLayout(infoBean: QueryInfoBean): QueryTagLayout {
        return QueryTagLayout.create(context, infoBean, mITagEvent)
    }
 
 
 
    private fun addSelectTag(tagInfo: QueryTagBean) {
        if (queryList.contains(tagInfo)) {
            return
        }
        if (queryList.isEmpty()) {
            selectLayout.isVisible = true
        }
        queryList.add(tagInfo)
        val tagView = QueryTagView.create(context, tagInfo, true)
        tagView.setTag(R.id.tag_query, tagInfo.code)
        tagView.setTagEvent(mITagEvent)
        selectFlexBox.addView(tagView)
    }
 
    private fun removeSelectTag(tagInfo: QueryTagBean) {
        if (!queryList.contains(tagInfo)) {
            return
        }
 
        var tagView: View? = null
        selectFlexBox.run {
            for (i in 0 until childCount) {
                if (getChildAt(i).getTag(R.id.tag_query).equals(tagInfo.code)) {
                    tagView = getChildAt(i)
                    break
                }
            }
        }
        if (tagView != null) {
            selectFlexBox.removeView(tagView)
            queryList.remove(tagInfo)
        }
 
        if (queryList.isEmpty()) {
            selectLayout.visibility = GONE
        }
    }
 
 
    private inner class MyTagEvent : ITagEvent {
 
        override fun onAdd(tagData: QueryTagBean) {
            addSelectTag(tagData)
        }
 
        override fun onRemove(tagData: QueryTagBean) {
            removeSelectTag(tagData)
        }
 
    }
 
 
 
    fun getQueryTags() = queryList
 
 
}