课堂笔记
编写缓存工具类
- 新建工具类 JsonCacheUtil ,设置成单例,并初始化 SharedPreferences mSharedPreferences 和 Gson mGson
public class JsonCacheUtil {
public static final String JSON_CACHE_SP_NAME = "json_cache_sp_name";
private final SharedPreferences mSharedPreferences;
private static JsonCacheUtil sJsonCacheUtil = null;
private final Gson mGson;
public static JsonCacheUtil getJsonCacheUtil() {
if (sJsonCacheUtil == null) {
return new JsonCacheUtil();
}
return sJsonCacheUtil;
}
private JsonCacheUtil() {
mSharedPreferences = BaseApplication.getAppContext().getSharedPreferences(JSON_CACHE_SP_NAME, Context.MODE_PRIVATE);
mGson = new Gson();
}
}
- 保存缓存的方法 saveCache,并重载带时间间隔参数的方法
public void saveCache(String key, Object value) {
saveCache(key, value, -1L);
}
public void saveCache(String key, Object value, long duration) {
SharedPreferences.Editor edit = mSharedPreferences.edit();
// object 转字符串
String valueStr = mGson.toJson(value);
if (duration != -1L) {
// 记录现在时间和时间间隔之和
duration += System.currentTimeMillis();
}
// 保存一个有数据有时间的内容
CacheWithDuration cacheWithDuration = new CacheWithDuration(duration, valueStr);
String cacheWithTime = mGson.toJson(cacheWithDuration);
// 保存 CacheWithDuration 对象的字符形式到 sp
edit.putString(key, cacheWithTime);
edit.apply();
}
- 其中类 CacheWithDuration 内容如下
public class CacheWithDuration {
private long duration;
private String cache;
public CacheWithDuration(long duration, String valueStr) {
this.cache = valueStr;
this.duration = duration;
}
public String getCache() {
return cache;
}
public void setCache(String cache) {
this.cache = cache;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
}
public void delCache(String key) {
mSharedPreferences.edit().remove(key).apply();
}
public <T> T getCache(String key, Class<T> clazz) {
// 取出 sp 里的数据
String valueWithDuration = mSharedPreferences.getString(key, null);
if (valueWithDuration == null) {
return null;
}
// 把字符串转换为 CacheWithDuration 对象
CacheWithDuration cacheWithDuration = mGson.fromJson(valueWithDuration, CacheWithDuration.class);
long duration = cacheWithDuration.getDuration();
// 这其实是判断从存储缓存到取出缓存的时间间隔和设置的时间间隔的大小
if (duration != -1 && duration - System.currentTimeMillis() <= 0) {
// 过期了
return null;
} else {
// 没过期,返回一个Class类型的数据
return mGson.fromJson(cacheWithDuration.getCache(), clazz);
}
}
保存搜索历史数据
public class Histories {
private List<String> historiesList;
public List<String> getHistoriesList() {
return historiesList;
}
public void setHistoriesList(List<String> historiesList) {
this.historiesList = historiesList;
}
}
private final JsonCacheUtil mJsonCacheUtil;
private static final String KEY_HISTORY = "key_history";
private static final int CACHE_WORDS_MAX_SIZE = 10;
// 参数 history 是搜索词,就是之前用的 keyword
private void saveHistory(String history) {
Histories histories = mJsonCacheUtil.getCache(KEY_HISTORY, Histories.class);
// 如果已经存在,就干掉,然后再添加
List<String> historiesList = null;
if (histories != null && histories.getHistoriesList() != null) {
historiesList = histories.getHistoriesList();
if (historiesList.contains(history)) {
historiesList.remove(history);
}
}
// 去重完成
// 处理没有数据的情况
if (historiesList == null) {
historiesList = new ArrayList<>();
}
if (histories == null) {
histories = new Histories();
}
histories.setHistoriesList(historiesList);
// 添加记录
historiesList.add(history);
// 对个数进行限制
if (historiesList.size() > CACHE_WORDS_MAX_SIZE) {
historiesList = historiesList.subList(0, CACHE_WORDS_MAX_SIZE);
}
// 保存记录
mJsonCacheUtil.saveCache(KEY_HISTORY, histories);
}
@Override
public void doSearch(String keyword) {
// 当搜索内容变化时才进行保存
if (mCurrentKeyword == null || !mCurrentKeyword.equals(keyword)) {
saveHistory(keyword);
this.mCurrentKeyword = keyword;
}
.......
}
@Override
public void getHistories() {
Histories histories = mJsonCacheUtil.getCache(KEY_HISTORY, Histories.class);
if (mCallback != null && histories != null
&& histories.getHistoriesList() != null
&& histories.getHistoriesList().size() != 0) {
mCallback.onHistoriesLoaded(histories.getHistoriesList());
}
}
@Override
public void delHistories() {
mJsonCacheUtil.delCache(KEY_HISTORY);
}
搜索顶部布局
- 和首页顶部布局很相似,可以复制修改(和老师的有些出入)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:background="@drawable/shape_edit_box_bg"
android:hint="请输入您想找... 的宝贝"
android:paddingLeft="11dp"
android:paddingRight="11dp"
android:textSize="12sp" />
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:gravity="center"
android:text="取消"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<FrameLayout
android:id="@+id/base_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
视频链接