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
|
|
|
}
|