1.依赖
//流式布局
api 'com.hyman:flowlayout-lib:1.1.2'
2.布局
<TextView
android:id="@+id/zuijin_sousuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最近搜索"
android:textSize="@dimen/dp_14"
android:textColor="#6666"
/>
<com.zhy.view.flowlayout.TagFlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/flowlayout"
android:is_limit ="true" true表示限制隐藏,false表示不限制行数
android:limit_line_count="3" 表示最大展示行,这里设置展示3行
/>
tv.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_17"
android:layout_marginLeft="@dimen/dp_10"
android:id="@+id/text_liu"
/>
3.activity
flowLayout = findViewById(R.id.flowlayout);//布局
find_find_find = findViewById(R.id.find_find_find);//搜索按钮
//点击搜索
private LayoutInflater from;
private List<String> list = new ArrayList<>();
------------handler线程---------------
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//设置流式布局adapter,【添加<String>(list)和更改Object为String】。
flowLayout.setAdapter(new TagAdapter<String>(list) {
@Override
public View getView(FlowLayout parent, int position, String o) {
TextView textView = (TextView) from.inflate(R.layout.tv,flowLayout,false);
textView.setText(o);
return textView;
}
});
}
};
from = LayoutInflater.from(this);//流式布局
find_find_find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.add(s);//集合---s代表输入框输入的字
handler.sendEmptyMessageDelayed(1,0);//线程发送消息
}
});