多选的效果图:
核心代码如下:
class MainActivity : AppCompatActivity() {
private val mPopupWindow: FilterPopupWindow by lazy { FilterPopupWindow(this) }
private val mList = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mList.apply {
add("apple")
add("banana")
add("peach")
add("cherry")
add("watermelon")
}
mPopupWindow.initScreenOrderStates(mList)
btn_filter.setOnClickListener {
mPopupWindow.showAsDropDown(btn_filter, 0, dip(1))
}
mPopupWindow.setOnDismissListener {
tv_result.setText("选择的结果是: "+mPopupWindow.mStates.joinToString(","))
}
}
override fun onDestroy() {
super.onDestroy()
if (mPopupWindow.isShowing) {
mPopupWindow.dismiss()
}
}
}
/**
* 筛选的PopupWindow
*/
class FilterPopupWindow(context: Context) : BaseOrderScreenPopupWindow(context) {
override fun initListener() {
super.initListener()
mBtnSure.setOnClickListener {
mStates.clear()
mStates.addAll(mEqualContainer.getCurrentStates())
Toast.makeText(mContext,mStates.toString(),Toast.LENGTH_LONG)
dismiss()
}
}
/**
* 加载需要筛选的状态
*/
override fun initScreenOrderStates(mScreenList: List<String>?) {
mFlexBoxLayout.removeAllViews()
//默认的不限标签
val notLimit = OrderViewCenter.createOrderStateTagView(mContext, "不限")
mFlexBoxLayout.addView(notLimit)
val states = mScreenList?.filter {
it.isNotNullOrEmpty() && it != "不限"
}
states?.apply {
forEach { state ->
val tagView = OrderViewCenter.createOrderStateTagView(mContext, state)
mFlexBoxLayout.addView(tagView)
}
}
val childMap = HashMap<String, TextView>()
mFlexBoxLayout.forEachChild { view ->
val tag = view.tag as String
childMap[tag] = view as TextView
Log.i("TAG",tag)
}
mEqualContainer = EqualContainerManager(childMap, "不限", object : BaseContainerManager.OnChildChangeListener<TextView> {
override fun onChildSelect(child: TextView) {
child.backgroundResource = R.drawable.shape_stroke_blue_order_select
child.textColorResource = R.color.blue_6d86d4
}
override fun onChildNormal(child: TextView) {
child.backgroundResource = R.drawable.shape_corner_gray_fafafa
child.textColorResource = R.color.text_color_light_blue
}
})
mEqualContainer.mUnLimitedState = "不限"
//设置默认状态
mStates.addAll(mEqualContainer.getCurrentStates())
//设置监听
mFlexBoxLayout.forEachChild { view ->
view.setOnClickListener {
mEqualContainer.updateState(view.tag as String)
}
}
}
}
object OrderViewCenter{
/**
* 创建筛选标签
*/
fun createOrderStateTagView(context: Context, state: String): TextView {
val stateTv = TextView(context)
val param = FlexboxLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
param.setMargins(context.dip(8),
context.dip(4),
context.dip(8),
context.dip(4))
stateTv.layoutParams = param
stateTv.text = state
stateTv.tag = state
stateTv.setPadding(context.dip(10),
context.dip(6),
context.dip(10),
context.dip(6))
stateTv.textSize = 12f
stateTv.backgroundResource = R.drawable.shape_corner_gray_fafafa
stateTv.textColorResource = R.color.text_color_light_blue
return stateTv
}
}
单选的效果图:
核心代码如下:
class SingleSelectActivity : AppCompatActivity() {
private val mApplyMap: HashMap<String, TextView> = HashMap()
private val mApply = arrayOf("全部", "apple", "banana", "peach", "cherry","watermelon")
private var mApplyContainer: MutexContainerManager<String, TextView>? = null
private var mSelectApplyType = "全部"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_single_select)
initApplyFlexLayout()
}
private fun initApplyFlexLayout() {
flexApplyLayout.removeAllViews()
mApply.apply {
forEach { type ->
val tagView = createFilterTagView2(type)
flexApplyLayout.addView(tagView)
}
}
flexApplyLayout.forEachChild { view ->
val tag = view.tag as String
mApplyMap[tag] = view as TextView
setApplyChildClick(view)
}
mApplyContainer = MutexContainerManager(mApplyMap, object : BaseContainerManager.OnChildChangeListener<TextView> {
override fun onChildSelect(child: TextView) {
child.backgroundResource = R.drawable.shape_corner_blue_2dp
child.textColorResource = R.color.blue_3548C3
}
override fun onChildNormal(child: TextView) {
child.backgroundResource = R.drawable.shape_corner_white_2dp
child.textColorResource = R.color.blue_2C365E
}
})
//默认选中第一项
flexApplyLayout.getChildAt(0).performClick()
}
/**
* 创建申请标签(白/蓝标签)
*/
fun createFilterTagView2(state: String): TextView {
val width = windowManager.defaultDisplay.width
val drawerWidth = width / 4 * 3 - dip(48)
val tagWidth = drawerWidth / 3
val stateTv = TextView(this)
val param = FlexboxLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
param.setMargins(0, 0, dip(4), dip(4))
stateTv.layoutParams = param
stateTv.text = state
stateTv.tag = state
stateTv.width = tagWidth
stateTv.height = dip(36)
stateTv.gravity = Gravity.CENTER
stateTv.textSize = 13f
stateTv.backgroundResource = R.drawable.shape_corner_white_2dp
stateTv.textColorResource = R.color.blue_2C365E
return stateTv
}
private fun setApplyChildClick(child: TextView) {
val tag = child.tag as String
if (tag != null) {
child.setOnClickListener {
mApplyContainer?.updateState(tag)
tv_select.text = "选择的结果: ${mApplyContainer?.getCurrentState()}"
}
}
}
}
源码地址:https://download.csdn.net/download/jingerlovexiaojie/18475592