天天看點

曆史搜尋記錄SearchHistory該demo使用了自定義的流式布局+AutoCompleteTextView+SharedPreferences實作了各大APP常用到的搜尋曆史記錄的功能,代碼簡單實用。

該demo使用了自定義的流式布局+AutoCompleteTextView+SharedPreferences實作了各大APP常用到的搜尋曆史記錄的功能,代碼簡單實用。

廢話不多說,上效果圖為證:

曆史搜尋記錄SearchHistory該demo使用了自定義的流式布局+AutoCompleteTextView+SharedPreferences實作了各大APP常用到的搜尋曆史記錄的功能,代碼簡單實用。
曆史搜尋記錄SearchHistory該demo使用了自定義的流式布局+AutoCompleteTextView+SharedPreferences實作了各大APP常用到的搜尋曆史記錄的功能,代碼簡單實用。

上代碼:第一步:自定義流式布局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<List<View>> mAllChildViews = new ArrayList<>();
    /**
     * 每一行的高度
     */
    private List<Integer> 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<View> lineViews = new ArrayList<View>();
        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();

        ArrayAdapter<String> autoCompleteAdapter = new ArrayAdapter<String>(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"?>
<LinearLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            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" />

        <AutoCompleteTextView
            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" />

        <TextView
            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" />

    </LinearLayout>


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                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" />

            <View style="@style/VerticalLineStyle" />

            <cn.cnpp.searchhistory.ZFlowLayout
                android:id="@+id/keyword_fl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    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" />

                <ImageView
                    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" />

            </RelativeLayout>

            <cn.cnpp.searchhistory.ZFlowLayout
                android:id="@+id/history_fl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>      

主要的代碼就在這裡了,如果想看完整的代碼就點選下方的連結去下載下傳demo吧。

https://download.csdn.net/download/u012360948/10393547