流式佈局(按照新增內容的寬度)
阿新 • • 發佈:2018-12-15
MainActivity
public class MainActivity extends AppCompatActivity { private String[] strings=new String[]{"流感","咳嗽","過敏","發燒","感冒","溼疹","便祕","痔瘡","協和","鼻炎","失眠","通風","上火","腳氣"}; HotView hotView,history; EditText editText; SqliteDao dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dao=new SqliteDao(MainActivity.this); editText=findViewById(R.id.et); hotView=findViewById(R.id.hotview); history=findViewById(R.id.history); for(String str:strings){ TextView textView=new TextView(MainActivity.this); textView.setText(str); textView.setBackgroundResource(R.drawable.edit_bg); textView.setTextSize(24); hotView.addView(textView); } //查詢資料庫裡的資料 List<Bean> beans = dao.selAll(); if(beans.size()!=0){ for(Bean bean:beans){ TextView textView=new TextView(MainActivity.this); textView = new TextView(MainActivity.this); textView.setText(bean.getContent()); textView.setTag(bean.getUuid()); textView.setBackgroundResource(R.drawable.edit_bg); textView.setTextSize(24); history.addView(textView); //長按刪除 textView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { String tag= String.valueOf(v.getTag()); Log.i("TEST",tag+"--=-"); dao.del(tag); history.removeView(v); return true; } }); } } //點選搜尋圖片 新增資料 findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView=new TextView(MainActivity.this); textView=new TextView(MainActivity.this); textView.setText(editText.getText().toString()); textView.setBackgroundResource(R.drawable.edit_bg); textView.setTextSize(24); //判斷資料庫裡是否有此資料 有不新增 沒有就新增 boolean b = dao.sel(editText.getText().toString()); if(!editText.getText().toString().equals("") && b){ UUID uuid=UUID.randomUUID(); textView.setTag(uuid); Log.i("TEST",String.valueOf(uuid)); dao.add(editText.getText().toString(),String.valueOf(uuid)); history.addView(textView); } //長按刪除 textView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { String tag= String.valueOf(v.getTag()); dao.del(tag); history.removeView(v); return true; } }); } }); //點選清空所有 清空資料庫資料同時 檢視也清空 findViewById(R.id.delAll).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dao.delAll(); history.removeAllViews(); } }); } }
HotView
public class HotView extends LinearLayout { private int mChildMaxHeight; //邊距 private int mVSpace = 20; private int mHSpace = 20; public HotView(Context context) { super(context); } public HotView(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); //尋找孩子中最高的一個孩子,找到的值會放在mChildMaxHeight變數中 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 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+view.getMeasuredHeight()); left+=view.getMeasuredWidth()+mHSpace; } } 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(); } } } }
資料庫的建立
public class SqliteHelper extends SQLiteOpenHelper { public SqliteHelper(Context context) { super(context, "User.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { //建立表 db.execSQL("create table history(id integer primary key autoincrement," + "content text," + "uuid text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
資料庫資料的修改
public class SqliteDao {
private SQLiteDatabase database;
public SqliteDao(Context context) {
database=new SqliteHelper(context).getWritableDatabase();
}
//新增
public void add(String s, String string){
ContentValues values=new ContentValues();
values.put("content",s);
values.put("uuid",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, "content=?", new String[]{string}, null, null, null);
return query.getCount()==0?true:false;
}
//查詢全部
public List<Bean> selAll(){
List<Bean> list=new ArrayList<>();
Cursor query = database.query("history", null, null, null, null, null, null);
while(query.moveToNext()){
String content = query.getString(query.getColumnIndex("content"));
String uuid = query.getString(query.getColumnIndex("uuid"));
list.add(new Bean(content,uuid));
}
return list;
}
public void del(String tag) {
database.delete("history","uuid=?",new String[]{tag});
}
}
Bean類
public class Bean {
private String content;
private String uuid;
public Bean(String content, String uuid) {
this.content = content;
this.uuid = uuid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
acitivity_main.xml
<?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:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/add"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/search"
/>
<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/add"
/>
</android.support.constraint.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textFontWeight="500"
android:layout_marginLeft="20dp"
android:text="搜尋歷史"/>
<com.example.day04_demo01.view.HotView
android:id="@+id/history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<Button
android:id="@+id/delAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空記錄"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:text="熱門搜尋"/>
<com.example.day04_demo01.view.HotView
android:id="@+id/hotview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
</LinearLayout>
單個textview佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="1"
/>
</LinearLayout>
edit_bg.xml TextView背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000" />
<corners android:radius="10px" />
<stroke
android:width="1px"
android:color="#999999" />
</shape>
效果圖