本文实例为大家分享了FlowLayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下
效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示
首先需要导入一个模块下载地址
下载完这个工程后,需要将里面的flowlayout-lib导入到工程中,
导入工程的步骤:文件-新导入模块选中这个flowlayout-lib
导入完成后,在项目的build.gradle中对导入的模块进行依赖
编译项目(“:flowlayout-lib”)
activity_main.xml
& lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " android:取向=按怪薄? android:填充=" 16 dp " 工具:上下文=癱om.example.searchhistory.MainActivity”比; LinearLayout & lt; android: layout_width=" match_parent " android: layout_height=" wrap_content " android:重力=" center_vertical " 面向android:="水平"比; EditText & lt; android: id=癅 + id/美国” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 4 "/比; & lt;按钮 android: id=癅 + id/btn” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 1 " android:文本="搜索”/比; & lt;按钮 android: id=癅 + id/清楚” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 1 " android:文本="清空”/比; & lt;/LinearLayout> & lt;滚动视图 android: layout_width=" match_parent " android: layout_height=癿atch_parent”比; & lt; com.zhy.view.flowlayout.TagFlowLayout android: id=癅 + id/id_flowlayout” android: layout_width="宽和" android: layout_height=" wrap_content " 应用:max_select=" 1 "/比; & lt;/ScrollView> & lt;/LinearLayout>>之前tv.xml
& lt; TextView xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_marginLeft=" 5 dp " android: layout_marginRight=" 5 dp " android: layout_marginTop=" 10 dp” android:背景=" @drawable/tag_bg” android:文本=癏elloworld” android:输入textColor=" # 999999 " android: textSize=16 sp”比; & lt;/TextView>>之前可拉的下面创建
checked_bg.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android "比; & lt;固体android:颜色=" # ffffff "/比; & lt;角落android:=? dp/半径比; & lt;中风 android:宽度=" 1 dp " android:颜色=" # dddddd "/比; & lt;填充 android:底部=" 5 dp " android:左=" 14 dp " android:对=" 14 dp " android:顶级=" 5 dp/比; & lt;/shape>>之前normal_bg.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android "比; & lt;固体android:颜色=" # ffffff "/比; & lt;角落android:=? dp/半径比; & lt;中风 android:宽度=" 1 dp " android:颜色=" # dddddd "/比; & lt;填充 android:底部=" 5 dp " android:左=" 14 dp " android:对=" 14 dp " android:顶级=" 5 dp/比; & lt;/shape>>之前tag_bg.xml
FlowLayout流式布局实现搜索清空历史记录