内容:像淘宝搜索栏一样,将搜索过得内容保存并显示在搜索历史
代码如下:
布局:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.leixiansheng.localhistorysearch.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_margin="10dp">
<EditText
android:id="@+id/et_search"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:background="@drawable/search_background"
android:padding="5dp"/>
<TextView
android:id="@+id/tv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="搜索"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="搜索历史"
android:textSize="18sp" />
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"/>
<LinearLayout
android:id="@+id/ll_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
</LinearLayout>
</LinearLayout>
LocalSearchUtil
package com.example.leixiansheng.localhistorysearch;
import android.content.Context;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Leixiansheng on 2018/6/26.
*/
public class LocalSearchUtil {
private static final String LOCAL_SEARCH = "local_search";
public static List<String> getLocalSearch(Context context){
String longHistory = (String) SPUtils.getInstance(context).getString(LOCAL_SEARCH, "");
String[] tmpHistory = longHistory.split(","); //split后长度为1有一个空串对象
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));
if (historyList.size() == 1 && historyList.get(0).equals("")) { //如果没有搜索记录,split之后第0位是个空串的情况下
historyList.clear(); //清空集合,这个很关键
}
return historyList;
}
public static void saveLocalSearch(Context context, String inputText) {
SPUtils spUtils = SPUtils.getInstance(context);
if (TextUtils.isEmpty(inputText)) {
return;
}
String longHistory = spUtils.getString(LOCAL_SEARCH, ""); //获取之前保存的历史记录
String[] tmpHistory = longHistory.split(","); //逗号截取 保存在数组中
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory)); //将改数组转换成ArrayList
if (historyList.size() > 0) {
//移除之前重复添加的元素
for (int i = 0; i < historyList.size(); i++) {
if (inputText.equals(historyList.get(i))) {
historyList.remove(i);
break;
}
}
historyList.add(0, inputText); //将新输入的文字添加集合的第0位也就是最前面
if (historyList.size() > 10) {
historyList.remove(historyList.size() - 1); //最多保存10条搜索记录 删除最早搜索的那一项
}
//逗号拼接
StringBuilder sb = new StringBuilder();
for (int i = 0; i < historyList.size(); i++) {
sb.append(historyList.get(i) + ",");
}
//保存到sp
spUtils.putString(LOCAL_SEARCH, sb.toString());
} else {
//之前未添加过
spUtils.putString(LOCAL_SEARCH, inputText + ",");
}
}
}
Main
package com.example.leixiansheng.localhistorysearch;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.et_search)
EditText mEtSearch;
@BindView(R.id.ll_search)
LinearLayout mLlSearch;
@BindView(R.id.tv_search)
TextView mTvSearch;
private List<String> mSearchList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
loadSearch();
mTvSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = mEtSearch.getText().toString();
if (!TextUtils.isEmpty(s)) {
LocalSearchUtil.saveLocalSearch(MainActivity.this, s);
}
loadSearch();
}
});
}
private void loadSearch() {
mLlSearch.removeAllViews();
mSearchList = LocalSearchUtil.getLocalSearch(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < mSearchList.size(); i++) {
final TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(mSearchList.get(i));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click:"+textView.getText(),Toast.LENGTH_SHORT).show();
}
});
mLlSearch.addView(textView, params);
}
}
}