歷史搜索記錄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
 * @Authordengyalan
 * @Date2018/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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章