第三方库之 FlowLayout
2021-05-20 本文已影响0人
Kevin_小飞象
目前最新版本是 1.1.2。
GitHub:https://github.com/hongyangAndroid/FlowLayout
简介
[不再维护]Android流式布局,支持单选、多选等,适合用于产品标签等。
- 以setAdapter形式注入数据
- 直接设置selector为background即可完成标签选则的切换,类似CheckBox
- 支持控制选择的Tag数量,比如:单选、多选
- 支持setOnTagClickListener,当点击某个Tag回调
- 支持setOnSelectListener,当选择某个Tag后回调
- 支持adapter.notifyDataChanged
- Activity重建(或者旋转)后,选择的状态自动保存
配置
- 在 app/build.gradle 中添加依赖
implementation 'com.hyman:flowlayout-lib:1.1.2'
-
效果图
Screenshot_2021-05-20-13-42-50-832_com.hk.hksale.jpg
代码
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg_normal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:text="工单类型"
android:textStyle="bold"/>
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/flowlayout_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:text="紧急程度"
android:textStyle="bold"/>
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/flowlayout_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_cancel"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="取消"/>
<TextView
android:id="@+id/txt_ok"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:textColor="@color/btn_color"
android:padding="6dp"
android:text="确定"/>
</LinearLayout>
</LinearLayout>
- tag_txt_level.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:gravity="center"
android:text="确定你好"
android:background="@drawable/sel_tag_bg"
android:textColor="@color/black"
android:textSize="16sp">
</TextView>
- 重要代码
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<String>(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<String>(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);
}