1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/ettext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textFontWeight="500dp"
android:layout_marginLeft="20dp"
android:text="搜索历史"/>
<com.bw.ymy.demo09.CurstomLayout
android:id="@+id/fall"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/dell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginLeft="30dp"
android:text="热门"/>
<com.bw.ymy.demo09.CurstomLayout
android:id="@+id/hotview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
2.热门展示数据布局
<?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:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
3.主页面
package com.bw.ymy.demo09;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//向里面添加热门内容
private String[] desc=new String[]{"陈独秀","李云龙","和尚","秀芹","秀儿","是","陈独秀","陈独秀","陈独秀","陈独秀",};
CurstomLayout fall;
CurstomLayout hotview;
EditText et;
Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//dao层
dao= new Dao(MainActivity.this);
//获取资源id
fall=findViewById(R.id.fall);
et=findViewById(R.id.ettext);
hotview=findViewById(R.id.hotview);
//循环添加热门内容
for(String str:desc){
TextView textView=new TextView(MainActivity.this);
textView.setText(str);
textView.setTextSize(24);
hotview.addView(textView);
}
//添加到数据库
List<String> list=dao.selall();
if(list.size()!=0)
{
for (String string:list)
{
TextView tv=new TextView(MainActivity.this);
tv.setText(et.getText());
tv.setTextColor(Color.GREEN);
fall.addView(tv);
}
}
//点击添加按钮
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv=new TextView(MainActivity.this);
tv.setText(et.getText());
tv.setTextColor(Color.GREEN);
//判断数据库是否有重复的 重复的加不进去
boolean b=dao.sel(et.getText().toString());
if(!et.getText().toString().equals("")&&b)
{
dao.add(et.getText().toString());
//没有重复的添加
fall.addView(tv);
}
}
});
///清空
findViewById(R.id.dell).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除数据库
dao.delall();
//删除列表
fall.removeAllViews();
}
});
}
}
4.View
package com.bw.ymy.demo09;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class CurstomLayout extends LinearLayout {
//孩子最高的一个
private int mChildMaxHeight;
//每一个左边距20 px
private int mHSpace=20;
//上下间距 20 px
private int mVSpace=20;
public CurstomLayout(Context context) {
super(context);
}
public CurstomLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//拿到父容器的宽高和计算模式
int sizeWidth=MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight=MeasureSpec.getSize(heightMeasureSpec);
//测量孩子大小,必须写
measureChildren(widthMeasureSpec,heightMeasureSpec);
//寻找孩子最高的一个
//在下方
findMaxChildMaxHeight();
//初始化值
int left=0,top=0;
//开始循环
int childCount=getChildCount();
for (int i=0;i<childCount;i++)
{
View view=getChildAt(i);
//是否是一行的第一个
if(left!=0)
{
//换行,放不下
if((left +view.getMeasuredWidth())>sizeWidth)
{
top+=mChildMaxHeight+mVSpace;
left=0;
}
}
left+=view.getMeasuredWidth()+mHSpace;
}
setMeasuredDimension(sizeWidth,(top+mChildMaxHeight)>sizeHeight?sizeHeight:top+mChildMaxHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findMaxChildMaxHeight();
//
//开始
//初始化
int left=0,top=0;
//循环
int childCount=getChildCount();
for (int i=0;i<childCount;i++)
{
View view=getChildAt(i);
//是否第一个
if(left !=0)
{
if((left+ view.getMeasuredWidth())>getWidth())
{
//计算下一行top
top+=mChildMaxHeight+mVSpace;
//换行
left=0;
}
}
//安排位置
view.layout(left,top,left+view.getMeasuredWidth(),top+getMeasuredHeight());
left+=view.getMeasuredWidth()+mHSpace;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
//z最高的孩子
private void findMaxChildMaxHeight() {
mChildMaxHeight=0;
int childCount =getChildCount();
for (int i=0;i<childCount;i++)
{
View view=getChildAt(i);
if(view.getMeasuredHeight()>mChildMaxHeight)
{
mChildMaxHeight=view.getMeasuredHeight();
}
}
}
}
5.Dao层
package com.bw.ymy.demo09;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class Dao {
private SQLiteDatabase database;
public Dao(Context context) {
database=new Sqlitehelp(context).getWritableDatabase();
}
//添加到数据库
public void add(String string)
{
ContentValues values=new ContentValues();
values.put("context",string);
database.insert("history",null,values);
}
//删除
public void delall()
{
database.delete("history",null,null);
}
public boolean sel(String string)
{
Cursor query = database.query("history", null, "context=?", new String[]{string}, null, null, null);
return query.getCount()==0?true:false;
}
//查询
public List<String> selall()
{
List<String> list=new ArrayList<>();
Cursor query = database.query("history", null, null, null, null, null, null);
while (query.moveToNext())
{
list.add(query.getString(query.getColumnIndex("context")));
}
return list;
}
}
6.sqlite创建数据库
package com.bw.ymy.demo09;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Sqlitehelp extends SQLiteOpenHelper {
public Sqlitehelp(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table history(id integer primary key autoincrement,"+"context text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
展示效果图
#热门的文字是死数据