废话不多说,上效果图为证:
上代码:第一步:自定义流式布局ZFlowLayout(让标签自动换行)
package cn.cnpp.searchhistory; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /***************************** * @Copyright(c) 2014-2018 * @Author:dengyalan * @Date:2018/1/16 * @Description:自定义搜索标签布局 * @Version:v1.0.0 *****************************/ public class ZFlowLayout extends ViewGroup { /** * 存储所有子View */ private List> mAllChildViews = new ArrayList<>(); /** * 每一行的高度 */ private List
mLineHeight = new ArrayList<>(); public ZFlowLayout(Context context) { this(context, null); } public ZFlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ZFlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //父控件传进来的宽度和高度以及对应的测量模式 int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); //如果当前ViewGroup的宽高为wrap_content的情况 //自己测量的宽度 int width = 0; //自己测量的高度 int height = 0; //记录每一行的宽度和高度 int lineWidth = 0; int lineHeight = 0; //获取子view的个数 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); //测量子View的宽和高 measureChild(child, widthMeasureSpec, heightMeasureSpec); //得到LayoutParams MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //子View占据的宽度 int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; //子View占据的高度 int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; //换行时候 if (lineWidth + childWidth > sizeWidth) { //对比得到最大的宽度 width = Math.max(width, lineWidth); //重置lineWidth lineWidth = childWidth; //记录行高 height += lineHeight; lineHeight = childHeight; } else {//不换行情况 //叠加行宽 lineWidth += childWidth; //得到最大行高 lineHeight = Math.max(lineHeight, childHeight); } //处理最后一个子View的情况 if (i == childCount - 1) { width = Math.max(width, lineWidth); height += lineHeight; } } //wrap_content setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width, modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mAllChildViews.clear(); mLineHeight.clear(); //获取当前ViewGroup的宽度 int width = getWidth(); int lineWidth = 0; int lineHeight = 0; //记录当前行的view List lineViews = new ArrayList (); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); //如果需要换行 if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) { //记录LineHeight mLineHeight.add(lineHeight); //记录当前行的Views mAllChildViews.add(lineViews); //重置行的宽高 lineWidth = 0; lineHeight = childHeight + lp.topMargin + lp.bottomMargin; //重置view的集合 lineViews = new ArrayList(); } lineWidth += childWidth + lp.leftMargin + lp.rightMargin; lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin); lineViews.add(child); } //处理最后一行 mLineHeight.add(lineHeight); mAllChildViews.add(lineViews); //设置子View的位置 int left = 0; int top = 0; //获取行数 int lineCount = mAllChildViews.size(); for (int i = 0; i < lineCount; i++) { //当前行的views和高度 lineViews = mAllChildViews.get(i); lineHeight = mLineHeight.get(i); for (int j = 0; j < lineViews.size(); j++) { View child = lineViews.get(j); //判断是否显示 if (child.getVisibility() == View.GONE) { continue; } MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int cLeft = left + lp.leftMargin; int cTop = top + lp.topMargin; int cRight = cLeft + child.getMeasuredWidth(); int cBottom = cTop + child.getMeasuredHeight(); //进行子View进行布局 child.layout(cLeft, cTop, cRight, cBottom); left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } left = 0; top += lineHeight; } } /** * 与当前ViewGroup对应的LayoutParams */ @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } }
第二步:新建一个SharePreference工具类,用来保存、读取、清除历史搜索记录,这里贴上这三个方法的代码
/** * 保存搜索记录 * * @param keyword */ public void save(String keyword) { // 获取搜索框信息 SharedPreferences mysp = mContext.getSharedPreferences("search_history", 0); String old_text = mysp.getString("history", ""); // 利用StringBuilder.append新增内容,逗号便于读取内容时用逗号拆分开 StringBuilder builder = new StringBuilder(old_text); builder.append(keyword + ","); // 判断搜索内容是否已经存在于历史文件,已存在则不重复添加 if (!old_text.contains(keyword + ",")) { SharedPreferences.Editor myeditor = mysp.edit(); myeditor.putString("history", builder.toString()); myeditor.commit(); } } public String[] getHistoryList() { // 获取搜索记录文件内容 SharedPreferences sp = mContext.getSharedPreferences("search_history", 0); String history = sp.getString("history", ""); // 用逗号分割内容返回数组 String[] history_arr = history.split(","); // 保留前50条数据 if (history_arr.length > 50) { String[] newArrays = new String[50]; System.arraycopy(history_arr, 0, newArrays, 0, 50); } return history_arr; } /** * 清除搜索记录 */ public void cleanHistory() { SharedPreferences sp = mContext.getSharedPreferences("search_history", 0); SharedPreferences.Editor editor = sp.edit(); editor.clear(); editor.commit(); }
最后呢,就到了关键的一步啦,在Activity里进行读写初始化操作啦。
package cn.cnpp.searchhistory; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.TextView; import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; /** * @author dengyalan */ public class MainActivity extends AppCompatActivity { @BindView(R.id.auto_search) AutoCompleteTextView autoSearch; @BindView(R.id.keyword_fl) ZFlowLayout keywordFl; @BindView(R.id.history_fl) ZFlowLayout historyFl; public static String[] searchWord = {"净水器", "手机", "电动车", "洗衣机", "沙发", "冰箱", "瓷砖", "空调", "床垫", "卫浴", "热水器", "床", "家具", "手表", "电视", "集成灶", "领带", "保温杯", "童装", "自行车", "空气净化器", "地板", "硅藻泥", "油烟机", "智能家居"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initView(); } private void initView() { initKeyword(searchWord); initHistory(); String[] data = SPUtils.getInstance(this).getHistoryList(); ArrayAdapterautoCompleteAdapter = new ArrayAdapter (this, R.layout.view_mw_textview, data); autoSearch.setAdapter(autoCompleteAdapter); autoSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (charSequence.length() > 0) { } } @Override public void afterTextChanged(Editable editable) { } }); } private void initHistory() { final String[] data = SPUtils.getInstance(this).getHistoryList(); ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(10, 10, 10, 10); historyFl.removeAllViews(); for (int i = 0; i < data.length; i++) { if (isNullorEmpty(data[i])) { return; } final int j = i; //添加分类块 View paramItemView = getLayoutInflater().inflate(R.layout.adapter_search_keyword, null); TextView keyWordTv = paramItemView.findViewById(R.id.tv_content); keyWordTv.setText(data[j]); keyWordTv.setBackgroundResource(R.drawable.whitebg_strokegrey_radius3); historyFl.addView(paramItemView, layoutParams); keyWordTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoSearch.setText(data[j]); } }); } } private void initKeyword(final String[] keyword) { ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(10, 10, 10, 10); keywordFl.removeAllViews(); for (int i = 0; i < keyword.length; i++) { final int j = i; //添加分类块 View paramItemView = getLayoutInflater().inflate(R.layout.adapter_search_keyword, null); TextView keyWordTv = paramItemView.findViewById(R.id.tv_content); keyWordTv.setText(keyword[j]); keyWordTv.setBackgroundResource(R.drawable.whitebg_strokegrey_radius3); keywordFl.addView(paramItemView, layoutParams); keyWordTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoSearch.setText(keyword[j]); } }); } } @OnClick({R.id.iv_back, R.id.clear_iv, R.id.tv_search}) public void onClick(View view) { switch (view.getId()) { case R.id.iv_back: finish(); break; case R.id.tv_search: String searchKey = autoSearch.getText().toString(); if (!isNullorEmpty(searchKey)) { Intent intent = new Intent(MainActivity.this, SearchResultActivity.class); intent.putExtra("key", searchKey); startActivityForResult(intent, 0); String keyWord = autoSearch.getText().toString(); if (!isNullorEmpty(keyWord)) { SPUtils.getInstance(MainActivity.this).save(autoSearch.getText().toString()); } } else { showToastShort(this, "搜索内容为空!"); } break; case R.id.clear_iv: SPUtils.getInstance(MainActivity.this).cleanHistory(); showToastShort(this, "已清除历史记录!"); initHistory(); break; default: break; } } private boolean isNullorEmpty(String str) { return str == null || "".equals(str); } private void showToastShort(Context context, String data) { Toast toast = Toast.makeText(context, data, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { if (resultCode == RESULT_OK) { initHistory(); } } } }
差点忘记还有布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> android:layout_width="match_parent" android:layout_height="45dp" android:background="@color/colorPrimary" android:gravity="center" android:orientation="horizontal"> android:id="@+id/iv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="3dp" android:padding="10dp" android:src="@mipmap/back" /> android:id="@+id/auto_search" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/whitebg_radius3" android:completionThreshold="1" android:drawableLeft="@mipmap/search_icon" android:drawablePadding="5dp" android:hint="请输入您要搜索的内容" android:padding="8dp" android:singleLine="true" android:textSize="12sp" /> android:id="@+id/tv_search" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:paddingLeft="10dp" android:paddingRight="15dp" android:text="搜索" android:textColor="@color/white" android:textSize="13dp" /> android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none"> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:drawableLeft="@mipmap/fire_grey" android:drawablePadding="3dp" android:text="热门搜索" android:textColor="@color/grey_4d4d4d" android:textSize="13sp" /> style="@style/VerticalLineStyle" /> android:id="@+id/keyword_fl" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:drawableLeft="@mipmap/history_grey" android:drawablePadding="3dp" android:text="搜索记录" android:textColor="@color/grey_4d4d4d" android:textSize="13sp" /> android:id="@+id/clear_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:padding="10dp" android:src="@mipmap/delete_grey" /> android:id="@+id/history_fl" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" />
主要的代码就在这里了,如果想看完整的代码就点击下方的链接去下载demo吧。
https://download.csdn.net/download/u012360948/10393547