布局:
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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" tools:context="com.example.day_0512_monizoukao.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="92px" android:gravity="center_vertical" android:focusable="true" android:padding="6px" android:focusableInTouchMode="true" android:orientation="horizontal"> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_margin="6px" android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false" android:hint="请输入搜素内容" android:id="@+id/ed_search" android:paddingBottom="10px" android:paddingLeft="30px" android:paddingTop="10px" android:textColor="#333" android:textColorHint="#666" android:textSize="12sp"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/tv_search" android:text="搜索" android:layout_marginRight="20px" android:layout_marginLeft="16px" android:gravity="center" android:textColor="#000fff" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="热搜" android:textSize="20sp"/> <com.example.day_0512_monizoukao.FlowLayout android:id="@+id/flow_layout" android:layout_width="match_parent" android:layout_height="50dp"> </com.example.day_0512_monizoukao.FlowLayout> <!-- <com.example.zhoukao1.FlowLayout android:id="@+id/flow_layout" android:layout_width="match_parent" android:layout_height="100dp"> </com.example.zhoukao1.FlowLayout>--> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="20px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="历史搜索" android:textSize="20sp"/> <com.example.day_0512_monizoukao.MyListView android:id="@+id/lv_search" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.day_0512_monizoukao.MyListView> <!-- <com.example.zhoukao1.MyListView android:id="@+id/lv_search" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.zhoukao1.MyListView>--> </LinearLayout> <Button android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空历史搜索" android:id="@+id/btn_delete"/> </LinearLayout> </ScrollView>
MainActivity 类:
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText etSearch; private TextView tvSearch; private Button btn_delete; private RecordSQLiteOpenHelper helper; private SimpleCursorAdapter adapter; private SQLiteDatabase db; private ListView lvSearch; private FlowLayout flowLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 etSearch = (EditText) findViewById(R.id.ed_search); tvSearch = (TextView) findViewById(R.id.tv_search); btn_delete = (Button) findViewById(R.id.btn_delete); lvSearch = (ListView) findViewById(R.id.lv_search); //初始化监听 initListener(); //初始化数据 initData(); //流式布局 initChildViews(); } //流式布局 private void initChildViews() { flowLayout = (FlowLayout) findViewById(R.id.flow_layout); ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.leftMargin = 5; lp.rightMargin = 5; lp.topMargin = 5; lp.bottomMargin = 5; for(int i = 0; i < datas.length; i ++){ TextView view = new TextView(this); view.setText(datas[i]); view.setTextColor(Color.WHITE); view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg)); flowLayout.addView(view,lp); } } private void initListener() { etSearch.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { etSearch.setFocusable(true); etSearch.setFocusableInTouchMode(true); tvSearch.setVisibility(View.VISIBLE); return false; } }); tvSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean hasData = hasData(etSearch.getText().toString().trim()); if (!hasData) { insertData(etSearch.getText().toString().trim()); queryData(""); } etSearch.setFocusable(false); etSearch.setFocusableInTouchMode(false); etSearch.clearFocus(); tvSearch.setVisibility(View.GONE); String searchContent = etSearch.getText().toString().trim(); etSearch.setText(""); } }); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteData(); queryData(""); } }); } private void deleteData() { db = helper.getWritableDatabase(); db.execSQL("delete from records"); db.close(); } private boolean hasData(String tempName) { Cursor cursor = helper.getReadableDatabase().rawQuery( "select id as _id,name from records where name =?", new String[]{tempName}); //判断是否有下一个 return cursor.moveToNext(); } private String datas[] = { "笔记本","空气净化器","安卓手机", "超级大号圆珠笔","空气滤芯","超级大号钢笔", }; private void initData() { initSearch(); helper = new RecordSQLiteOpenHelper(this); queryData(""); } private void initSearch() { for (int i = 0; i < datas.length; i++) { final TextView textView = new TextView(this); textView.setText(datas[i]); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String searchContent = textView.getText().toString(); Toast.makeText(MainActivity.this, "" + searchContent, Toast.LENGTH_SHORT).show(); etSearch.setText(""); } }); } } private void queryData(String tempName) { Cursor cursor = helper.getReadableDatabase().rawQuery( "select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null); adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{"name"}, new int[]{android.R.id.text1}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lvSearch.setAdapter(adapter); adapter.notifyDataSetChanged(); } private void insertData(String tempName) { db = helper.getWritableDatabase(); db.execSQL("insert into records(name) values('" + tempName + "')"); db.close(); } }
FlowLayout 类:
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /** * Created by MSI on 2018/5/13. */ public class FlowLayout extends ViewGroup{ //存储所有子View private List<List<View>> mAllChildViews = new ArrayList<>(); //每一行的高度 private List<Integer> mLineHeight = new ArrayList<>(); public FlowLayout(Context context) { this(context, null); // TODO Auto-generated constructor stub } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub //父控件传进来的宽度和高度以及对应的测量模式 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) 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); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub 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 ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { // TODO Auto-generated method stub return new MarginLayoutParams(getContext(), attrs); } }
RecordSQLiteOpenHelper 类:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by MSI on 2018/5/13. */ public class RecordSQLiteOpenHelper extends SQLiteOpenHelper { private static String name = "temp.db"; private static Integer version = 1; public RecordSQLiteOpenHelper(Context context) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table records(id integer primary key autoincrement,name varchar(200))"); db.insert("热水器", "records", null); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
MyListView 类:
import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /** * Created by MSI on 2018/5/13. */ public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 重新计算高度 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
R.drawable.textview_bg:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="#999"/> <size android:height="10dp" android:width="10dp"/> </shape>