1.内容视图
public class WaterFallItem extends LinearLayout {
private int mChildMacHeigh;
private int mHSpace = 20;
private int mVSpace = 20;
public WaterFallItem(Context context) {
super(context);
}
public WaterFallItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获得父容器的宽高
int width = MeasureSpec.getSize(widthMeasureSpec);
int heigh = MeasureSpec.getSize(heightMeasureSpec);
//测量孩子的宽高
measureChildren(widthMeasureSpec,heightMeasureSpec);
findChildMaxHeigh();
int left = 0,top = 0;
int mCount = getChildCount();
for (int i = 0; i < mCount; i++) {
View view = getChildAt(i);
if(left != 0){
if((left+view.getMeasuredWidth()) > width){
top += mChildMacHeigh+mVSpace;
left = 0;
}
}
left += view.getMeasuredWidth() +mHSpace;
}
//测量
setMeasuredDimension(width,(top + mChildMacHeigh) > heigh ? heigh : top+mChildMacHeigh);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findChildMaxHeigh();
int left = 0,top = 0;
int mCount = getChildCount();
for (int i = 0; i < mCount; i++) {
View view = getChildAt(i);
if(left != 0){
if((left+view.getMeasuredWidth()) > getWidth()){
top += mChildMacHeigh+mVSpace;
left = 0;
}
}
//安排孩子的位置
view.layout(left,top,left+view.getMeasuredWidth(),top + mChildMacHeigh);
left += view.getMeasuredWidth() +mHSpace;
}
}
private void findChildMaxHeigh() {
int count = getChildCount();
mChildMacHeigh = 0;
for (int i = 0; i < count; i++) {
View view = getChildAt(i);
if (view.getMeasuredHeight() > mChildMacHeigh){
mChildMacHeigh = view.getMeasuredHeight();
}
}
}
}
2.搜索框
public class WaterFallTitle extends LinearLayout {
Context context;
public WaterFallTitle(Context context) {
super(context);
this.context = context;
init();
}
public WaterFallTitle(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
View view = View.inflate(context,R.layout.water_itemtitle,null);
final EditText editText = view.findViewById(R.id.editText);
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(buttonClickLister != null){
buttonClickLister.OnButtonClick(editText.getText().toString());
}
}
});
addView(view);
}
//定义接口传得到editText的值
OnButtonClickLister buttonClickLister;
public void setOnButtonClickLister(OnButtonClickLister clickLister){
buttonClickLister = clickLister;
}
public interface OnButtonClickLister{
void OnButtonClick(String str);
}
}
3.标题
@SuppressLint("AppCompatCustomView")
public class WaterFallText extends TextView {
public WaterFallText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.KongJian);
int color = typedArray.getColor(R.styleable.kjColor_textcolor, Color.BLACK);
setTextColor(color);
//回收
typedArray.recycle();
}
}
3.1 attrs内容(Values下创建)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="KongJian">
<attr name="text" format="string"/>
</declare-styleable>
<declare-styleable name="kjColor">
<attr name="textcolor" format="color"/>
</declare-styleable>
</resources>
4.MainXml布局
<?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">
<com.example.tiamo.threeday.View.WaterFallTitle
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.example.tiamo.threeday.View.WaterFallText
android:id="@+id/ssls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索历史"
/>
<com.example.tiamo.threeday.View.WaterFallItem
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.example.tiamo.threeday.View.WaterFallText
android:id="@+id/Remen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="热门"
/>
<com.example.tiamo.threeday.View.WaterFallItem
android:id="@+id/item_texts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
5.MainActivity
public class MainActivity extends AppCompatActivity {
private Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
final WaterFallItem Search_history = findViewById(R.id.item_text);
WaterFallItem hot = findViewById(R.id.item_texts);
dao = new Dao(this);
WaterFallTitle title = findViewById(R.id.title);
//查询数据库
final List<UserBean> query = dao.query();
for (int i = 0; i < query.size(); i++) {
TextView tv = new TextView(this);
tv.setText(query.get(i).getTitle());
tv.setTextColor(Color.RED);
tv.setBackgroundResource(R.drawable.edit_bg);
Search_history.addView(tv);
final int index = i;
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(query.get(index).getUuid());
Search_history.removeView(v);
}
});
}
title.setOnButtonClickLister(new WaterFallTitle.OnButtonClickLister() {
@Override
public void OnButtonClick(String str) {
UUID uuid = UUID.randomUUID();
TextView tv = new TextView(MainActivity.this);
tv.setTag(uuid);
tv.setTextColor(Color.RED);
tv.setText(str);
tv.setBackgroundResource(R.drawable.edit_bg);
Search_history.addView(tv);
dao.insert(uuid.toString(),str);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(v.getTag().toString());
Search_history.removeView(v);
}
});
}
});
//热门下内容
for (int i = 0; i < 30; i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText("数据 " + i);
tv.setTextColor(Color.RED);
tv.setBackgroundResource(R.drawable.edit_bg);
hot.addView(tv);
}
}
}
5.1内容背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fcc"/>
<corners android:radius="10px"/>
<stroke android:width="1px"
android:color="#999"/>
</shape>
6.数据库
public class MySQL extends SQLiteOpenHelper {
public MySQL( Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(id integer primary key autoincrement," +
"title text," +
"uuid text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
6.0Dao层
public class Dao {
MySQL mySQL;
SQLiteDatabase database;
public Dao(Context context) {
mySQL = new MySQL(context);
database = mySQL.getReadableDatabase();
}
public void insert(String uuid,String title){
ContentValues values = new ContentValues();
values.put("title",title);
values.put("uuid",uuid);
database.insert("user",null,values);
}
public void del(String uuid){
database.delete("user","uuid = ?",new String[]{uuid});
}
public List<UserBean> query(){
List<UserBean> list = new ArrayList<>();
Cursor query = database.query("user", null, null, null, null, null, null);
while (query.moveToNext()){
String title = query.getString(query.getColumnIndex("title"));
String uuid = query.getString(query.getColumnIndex("uuid"));
UserBean bean = new UserBean(title,uuid);
list.add(bean);
}
return list;
}
}
7.Bean类
public class UserBean {
private String title;
private String uuid;
public UserBean(String title, String uuid) {
this.title = title;
this.uuid = uuid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}