多样可折叠和展开的标签控件 ,MultiTagFlowLayout相当于一个容器一样, 支持RecyclerView和FlowLayout组件,RecyclerView 只支持垂直九宫格和垂直列表,和RecyclerView 使用一样;FlowLayout 流式布局组件,支持标签,标签单选,标签多选等功能!!!
##先看两张图
![](https://private-user-images.githubusercontent.com/25949241/256115068-d5402aa2-469e-4df1-9af9-12f0f87ed85b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDg0MjcsIm5iZiI6MTcyNDE0ODEyNywicGF0aCI6Ii8yNTk0OTI0MS8yNTYxMTUwNjgtZDU0MDJhYTItNDY5ZS00ZGYxLTlhZjktMTJmMGY4N2VkODViLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDEwMDIwN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkzMzhhZmNlMjE5ZDBkYzA3OGNjZDI1OTNlMzkwOTIxM2QxY2VlZTg3MDkzNzdhYWRhY2U5ZDM3Y2M2NzkzMmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.axTnNJKub5Qk0zsSGaImnGoqUQko7NDqWk6K2T1DuNs)
![](https://private-user-images.githubusercontent.com/25949241/256115696-f9ef28bb-9895-4d2d-b753-a07e7189095b.jpg?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDg0MjcsIm5iZiI6MTcyNDE0ODEyNywicGF0aCI6Ii8yNTk0OTI0MS8yNTYxMTU2OTYtZjllZjI4YmItOTg5NS00ZDJkLWI3NTMtYTA3ZTcxODkwOTViLmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDEwMDIwN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJlM2FlMjlhYTI2NWNkOGQ0ZTc2ZTcyMGU0M2JlOWNiODBlZGU3YWNmYzc3ZDE3NDNhOGY3NTVmOTI1YzEzMGEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.lfEf6dKuDrLNXuuIphUF2YvJOevYDVql_rvuekrQ1Tc)
dependencies {
implementation 'com.github.XueyiXia:TagFlowLayout:v1.0.5'
}
-
定义xml布局
<com.framework.tagflow.MultiTagFlowLayout android:id="@+id/multi_flow_tag" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dp_10" android:layout_marginStart="@dimen/dp_10" android:layout_marginEnd="@dimen/dp_10" app:expandHint="@string/expandHint" app:foldHint="@string/foldHint" app:hasMore="true" app:layout_type="FlowLayout" app:tagsHorizontalSpace="@dimen/dp_5" app:tagsVerticalSpace="@dimen/dp_5"/>
-
初始化
mTestAdapter=TestAdapter(this); for(index in 0..10){ var bean= TestBean() mTestAdapter.addData(bean) } mViewBinding.multiFlowTag.setAdapter(mTestAdapter) mViewBinding.multiFlowTag.setOnTagClickListener(object :OnTagClickListener{ override fun onClick(view: View?, position: Int) { Toast.makeText(this@MainActivity,"点击事件-->>:$position",Toast.LENGTH_SHORT).show() } override fun onLongClick(view: View?, position: Int) { Toast.makeText(this@MainActivity,"长按事件-->>:$position",Toast.LENGTH_SHORT).show() } }) mViewBinding.multiFlowTag.setItemModel(MultiTagFlowLayout.ITEM_MODEL_SELECT) mViewBinding.multiFlowTag.setSelectedListener(object :OnTagSelectedListener{ override fun selected(view: View?, position: Int, selected: List<BaseTagBean?>?) { Toast.makeText(this@MainActivity,"selected事件-->>:$position",Toast.LENGTH_SHORT).show() } override fun unSelected(view: View?, position: Int, selected: List<BaseTagBean?>?) { Toast.makeText(this@MainActivity,"unSelected事件-->>:$position",Toast.LENGTH_SHORT).show() } })
}
1.DefaultTagView (默认实心tag)
2.ColorfulTagView (彩色背景实心tag)
3.StrokeTagView (空心带边框的tag)
4.ColorfulStrokeTagView (空心彩色边框tag)
自定义tag,继承以上tag或者自定义View
3可选项(部分属性可直接在xml布局中指定)
<declare-styleable name="TagFlowLayout">
<attr name="tagsHorizontalSpace" format="dimension" />
<attr name="tagsVerticalSpace" format="dimension" />
<attr name="animationDuration" format="integer" />
<attr name="hasMore" format="boolean" />
<attr name="foldHint" format="reference|string" />
<attr name="expandHint" format="reference|string" />
<!--布局类型,列表或者-->
<attr name="layout_type" format="reference">
<enum name="RecyclerView" value="0" />
<enum name="FlowLayout" value="1" />
</attr>
<!--LayoutManager 模式-->
<attr name="layoutManager_Mode" format="reference">
<enum name="LinearLayoutManager" value="0"/>
<enum name="GridLayoutManager" value="1"/>
</attr>
</declare-styleable>