//FrameLayout 里面的代码
public class LiuLayout extends FrameLayout {
private final int V_With=20;
public LiuLayout(Context context) {
super(context);
}
public LiuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LiuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void addTextView(String name) {
TextView textView = (TextView) View.inflate(getContext(), R.layout.liu_item, null);
textView.setText(name);
LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
addView(textView);
//Log.e("zmz", "添加成功!");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//获取本控件的宽度
int width = getWidth();
//行数
int row = 0;
//子控件开始坐标
int disWith = V_With;
for (int i = 0; i < getChildCount(); i++) {
//寻找子控件
View child = getChildAt(i);
//子控件的宽高
int childWidth = child.getWidth();
int childHeight = child.getHeight();
//判断是否满行,
if (disWith + childWidth > width) {
//如果满行,则换行
row++;
//子控件开始坐标还原
disWith = V_With;
}
child.layout(disWith, row * childHeight, disWith + childWidth, childHeight * (row + 1));
disWith += childWidth;
}
}
}
//Activity里面的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
LiuLayout liuLayout;
EditText editText;
Button button;
Dao dao;
ArrayList<String>list;
TextView del_textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//寻找控件
editText = findViewById(R.id.edi_name);
button = findViewById(R.id.button_ss);
liuLayout = findViewById(R.id.liuLayout);
del_textView = findViewById(R.id.del_text);
//点击事件
button.setOnClickListener(this);
del_textView.setOnClickListener(this);
//创建数据库
dao = new Dao(this);
//查询数据库数据添加到流式布局
initData();
}
private void initData() {
Cursor cursor = dao.sel("person",null,"",new String[]{},null,null,null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
liuLayout.addTextView(name);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_ss:
//获取数据添加到流式布局和数据库
addData();
break;
case R.id.del_text:
//删除,需要把数据库里的数据也要删除
delData();
break;
}
//清除输入框中的值
editText.getText().clear();
}
private void delData() {
//提示框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list = new ArrayList<String>();
//先查出有什么数据
Cursor cursor = dao.sel("person",null,"",new String[]{},null,null,null);
while (cursor.moveToNext()){
String names = cursor.getString(cursor.getColumnIndex("name"));
//存入list集合作为删除的字段
list.add(names);
}
//循环删除数据库的数据
for (int i = 0; i <list.size() ; i++) {
long delete = dao.del("person","name=?",new String[]{list.get(i)});
}
//删除流式布局的内容
liuLayout.removeAllViews();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了取消!", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
private void addData() {
//获取输入框中的值
String name = editText.getText().toString().trim();
//非空判断
if (name.isEmpty()) {
Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
return;
}
//添加到流式布局
liuLayout.addTextView(name);
//添加数据库
ContentValues values = new ContentValues();
values.put("name",name);
long insert = dao.add("person",null,values);
}
}
//dao层
public class Dao {
private SQLiteDatabase db;
public Dao(Context context) {
MHolder mHolder = new MHolder(context);
db = mHolder.getWritableDatabase();
}
public long add(String table, String nullColumnHack, ContentValues values) {
return db.insert(table,nullColumnHack,values);
}
public Cursor sel(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy){
return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
public long del(String table, String whereClause, String[] whereArgs) {
return db.delete(table,whereClause,whereArgs);
}
}
//布局
<?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="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/edi_name"
android:layout_width="300dp"
android:background="@drawable/sty_color"
android:hint="请输入......."
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="搜索"
android:background="@drawable/btn_color"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
android:text="搜索历史:"/>
<TextView
android:id="@+id/del_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:textSize="10sp"
android:text="清除历史记录"/>
</RelativeLayout>
<zhao.com.hy.LiuLayout
android:id="@+id/liuLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</zhao.com.hy.LiuLayout>
</LinearLayout>
//效果图