其他分享
首页 > 其他分享> > Android 实现搜索历史(2),apm性能监控系统

Android 实现搜索历史(2),apm性能监控系统

作者:互联网

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;

}

}

/**

*/

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

}

第二步:新建一个SharePreference工具类,用来保存、读取、清除历史搜索记录,这里贴上这三个方法的代码

/**

*/

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;

/**

*/

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 = {“净水器”, “手机”, “电动车”, "洗衣

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享

机", “沙发”, “冰箱”, “瓷砖”, “空调”, “床垫”, “卫浴”, “热水器”, “床”, “家具”, “手表”, “电视”, “集成灶”, “领带”, “保温杯”, “童装”, “自行车”, “空气净化器”, “地板”, “硅藻泥”, “油烟机”, “智能家居”};

@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 autoCompleteAdapter = 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"?>

<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” />

<android.support.v4.widget.NestedScrollView

android:layout_width=“match_parent”

标签:layout,int,void,id,搜索,apm,import,Android,android
来源: https://blog.csdn.net/m0_64319000/article/details/121437584