Skip to content

Android流式布局,支持单选、多选等,适合用于产品标签等。

License

Notifications You must be signed in to change notification settings

githubwwj/FlowLayout

Repository files navigation

FlowLayout

Android流式布局,支持单选、多选等,适合用于产品标签等。

特色

  • 以setAdapter形式注入数据
  • 直接设置selectorbackground即可完成标签选则的切换,类似CheckBox
  • 支持控制选择的Tag数量,比如:单选、多选
  • 支持setOnTagClickListener,当点击某个Tag回调
  • 支持setOnSelectListener,当选择某个Tag后回调
  • 支持 adapter.notifyDataChanged
  • Activity重建(或者旋转)后,选择的状态自动保存
  • 支持addTagList(List<T> tagList) 添加更多的标签数据
  • 支持清空选中的标签clearSelectView()
  • 达到最大选择标签数量时支持回调onMaxSelected()

效果图

image-20241008182743227

用法

dependencies{implementation'com.github.githubwwj:FlowLayout:v1.2' }

声明

布局文件中声明:

<wen.view.flowlayout.TagFlowLayoutandroid:id="@+id/id_flowlayout"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="20dp"wen:max_select="-1" />

支持属性:

  • max_select-1为不限制选择数量,>=1的数字为控制选择tag的数量
  • 支持通过state=checked来控制选中和取消,也可以自己在AdapteronSelected(int position, View view)unSelected(int position, View view)中分别处理显示。

设置数据

mFlowLayout.setAdapter(newTagAdapter<String>(mVals){@OverridepublicViewgetView(FlowLayoutparent, intposition, Strings){TextViewtv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(s); returntv} });

选中状态

<?xmlversion="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:drawable="@drawable/checked_bg"android:state_checked="true" /> <itemandroid:drawable="@drawable/normal_bg" /> </selector>

设置个标签的背景,一个状态为选中状态,另一个为正常状态。另一种写法,写在Java代码中:

publicvoidonSelected(intposition, Viewview){Log.d("FlowLayout", "onSelected " + position)} publicvoidunSelected(intposition, Viewview){Log.d("FlowLayout", "unSelected " + position)}

点击标签事件回调

mFlowLayout.setOnTagClickListener((view1, position, parent) ->{Toast.makeText(getActivity(), mVals[position], Toast.LENGTH_SHORT).show()});

选中多个标签回调

mFlowLayout.setOnSelectListener(newTagFlowLayout.OnSelectListener(){@OverridepublicvoidonSelected(Set<Integer> selectPosSet){getActivity().setTitle("choose:" + selectPosSet.toString())} });

预先选中某些标签

//预先设置选中mFlowLayout.setSelectedList(1, 3, 5, 7, 8, 9); //获得所有选中的标签位置副本mFlowLayout.getSelectedList();

About

Android流式布局,支持单选、多选等,适合用于产品标签等。

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages