目前最新版本是 1.1.2。 GitHub:https://github.com/hongyangAndroid/FlowLayout
一、 简介Android
流式布局,支持单选、多选等,适合用于产品标签等。
- 以
setAdapter
形式注入数据 - 直接设置
selector
为background
即可完成标签选则的切换,类似CheckBox
- 支持控制选择的
Tag
数量,比如:单选、多选 - 支持
setOnTagClickListener
,当点击某个Tag回调 - 支持
setOnSelectListener
,当选择某个Tag后回调 - 支持
adapter.notifyDataChanged
Activity
重建(或者旋转)后,选择的状态自动保存
1. 在 app/build.gradle 中添加依赖
implementation 'com.hyman:flowlayout-lib:1.1.2'
2. 效果图
1. 布局文件
2. tag_txt_level.xml
3. 重要代码
private void showPopupWindow() {
if (popupWindow != null && popupWindow.isShowing()) {
return;
}
popupWindow = new CommonPopupWindow.Builder(getActivity())
.setView(R.layout.popup_filter)
.setAnimationStyle(R.style.AnimDown)
.setBackGroundLevel(0.5f)
.setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
.setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
@Override
public void getChildView(View view, int layoutResId) {
final LayoutInflater mInflater = LayoutInflater.from(getActivity());
TagFlowLayout flowLayout = (TagFlowLayout) view.findViewById(R.id.flowlayout_type);
TagFlowLayout flowLayout_level = (TagFlowLayout) view.findViewById(R.id.flowlayout_level);
TextView txt_ok = (TextView) view.findViewById(R.id.txt_ok);
TextView txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
flowLayout.setMaxSelectCount(1);
flowLayout_level.setMaxSelectCount(1);
flowLayout.setAdapter(new TagAdapter(types) {
@Override
public View getView(FlowLayout parent, int position, String text) {
TextView tv_date = (TextView)mInflater.inflate(R.layout.tag_txt_level,flowLayout,false);
tv_date.setText(text);
return tv_date;
}
});
flowLayout_level.setAdapter(new TagAdapter(levels) {
@Override
public View getView(FlowLayout parent, int position, String text) {
TextView tv_date = (TextView)mInflater.inflate(R.layout.tag_txt,flowLayout_level,false);
tv_date.setText(text);
return tv_date;
}
});
flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
@Override
public boolean onTagClick(View view, int position, FlowLayout parent) {
ToastUtils.showToast(getActivity(),types.get(position));
return true;
}
});
flowLayout_level.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
@Override
public boolean onTagClick(View view, int position, FlowLayout parent) {
ToastUtils.showToast(getActivity(),levels.get(position));
return true;
}
});
txt_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow != null) {
popupWindow.dismiss();
}
}
});
txt_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// todo 接口文档
if (popupWindow != null) {
popupWindow.dismiss();
}
}
});
}
})
.setOutsideTouchable(true)
.create();
popupWindow.showAsDropDown(mTitleBar);
}