最近的项目需要做本地历史记录功能,以前写的有些过时了,就在网上查了查较好的开源库、实现方式等。最终选择了鸿洋大神的FlowLayout流式布局,再搭配SharedPreferencesUtil工具类来实现本地历史记录功能。同时FlowLayout开源库还非常适合实现产品标签(支持单选、多选)功能,所以在Demo中也有具体的使用,研究研究总是没错的。
开源库的下载、导入等操作就不再废话了,直接开始项目演示。
注:文章末尾附项目源码下载地址及FlowLayout开源库地址
效果展示
主要功能:FlowLayout的使用、从SP中读取历史记录、将历史记录写入到SP中、历史记录最大数量限制、历史记录不可重复、最新查询的在最前边、清楚历史记录;FlowLayout预先设置选中、设置最大选中数、设置标签点击和选中监听、获取选中的标签、通过selecter完成标签选择的切换等。方便演示加入了测试数据。
本地历史记录的实现
1.页面布局:主要是在布局文件中使用TagFlowLayout实现流失布局,并通过自定义属性(zhy:max_select="0")规定标签的最大可选数。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zhy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/titlebar_bg">
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@null">
<TextView
android:id="@+id/tv_titlebar_center"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:gravity="center"
android:maxLength="18"
android:singleLine="true"
android:text="商品查询"
android:textColor="@color/themeTextColor"
android:textSize="@dimen/themeTextSize" />
<ImageView
android:id="@+id/iv_titlebar_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@null"
android:padding="14dp"
android:src="@mipmap/titlebar_back" />
</RelativeLayout>
<RelativeLayout