瀑布流通过屏幕的宽高运用接口回调添加数据库+自定义view

在这里插入图片描述

Xml

<com.bwei.week01.CustomTitleView
        android:id="@+id/titleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="搜索记录"
    android:textSize="20dp"/>

<com.bwei.week01.CustomFlawView
    android:id="@+id/falwView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:text="热门搜索"
    />
<com.bwei.week01.CustomFlawView
    android:id="@+id/reView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

MainActivity

titleView=findViewById(R.id.titleView);
        flawView=findViewById(R.id.falwView);
        dao=new UserDao(this);
       CustomFlawView reView=findViewById(R.id.reView);
        titleView.setOnButtonClickListener(new CustomTitleView.setButtonClickListener() {
            @Override
            public void onSuccess(final String str) {
                //随机数当作唯一标识
                UUID uuid = UUID.randomUUID();
                TextView view = new TextView(MainActivity.this);
                view.setTag(uuid);
                view.setTextColor(Color.BLACK);
                view.setText(str);
                uuid1 = String.valueOf(view.getTag());
                dao.add(uuid1,str);
                view.setBackgroundResource(R.drawable.edit_bg);
                flawView.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    dao.del(uuid1);
                    flawView.removeView(v);
                }
            });
        }
    });

    for (int i=0;i<10;i++){
        TextView tv = new TextView(MainActivity.this);
        tv.setText("数据:" + i);
        tv.setTextColor(Color.RED);
        tv.setBackgroundResource(R.drawable.edit_bg);
        reView.addView(tv);
    }

titleView自定义

private Context mContext;


public CustomTitleView(Context context) {
    super(context);
    mContext=context;
    init();
}

public CustomTitleView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mContext=context;
    init();
}

private void init() {
    View view = View.inflate(mContext, R.layout.activity_title, null);
    final EditText editText = view.findViewById(R.id.editText);
    ImageView imageView = view.findViewById(R.id.imageView);
    imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            if(setButtonClickListener!=null){
                setButtonClickListener.onSuccess(editText.getText().toString());
            }

        }
    });
    addView(view);
}

setButtonClickListener setButtonClickListener;

public void setOnButtonClickListener(setButtonClickListener buttonClickListener){
    this.setButtonClickListener=buttonClickListener;
}


public interface setButtonClickListener{
    void onSuccess(String str);
}

添加纪录

private Context mContext;
    int mChildenerHeight;
    int mHeight=10;
    int mWidth=10;

public CustomFlawView(Context context) {
    super(context);
    mContext=context;
}

public CustomFlawView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mContext=context;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    //获取父窗口的
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int HeightSize = MeasureSpec.getSize(heightMeasureSpec);

    measureChildren(widthMeasureSpec,heightMeasureSpec);


    findData();

    int left=0,top=0;

    int childCount = getChildCount();
    for (int i=0;i<childCount;i++){
        View childAt = getChildAt(i);
        if(left!=0){
            if((left+childAt.getMeasuredWidth()>widthSize)){
                top+=mChildenerHeight+mHeight;
                left = 0;
            }
        }
        left+=childAt.getMeasuredWidth()+mWidth;
    }

    setMeasuredDimension(widthSize,(top+mChildenerHeight)>HeightSize?HeightSize:top+mChildenerHeight);

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    findData();
    int top=0,left=0;

    int childCount = getChildCount();
    for (int i=0;i<childCount;i++){
        View childAt = getChildAt(i);
        if(left!=0){
            if((left+childAt.getMeasuredWidth()>getWidth())){
                top+=mChildenerHeight+mHeight;
                left=0;
            }
        }
        childAt.layout(left,top,left+childAt.getMeasuredWidth(),top+childAt.getMeasuredHeight());
        left+=childAt.getMeasuredWidth()+mWidth;
    }

}

private void findData() {
    mChildenerHeight=0;
    int childCount = getChildCount();
    for (int i=0;i<childCount;i++){
        View childAt = getChildAt(i);
        if(childAt.getMeasuredHeight()>mChildenerHeight){
            mChildenerHeight=childAt.getMeasuredHeight();
        }
    }
}

数据库

public MySQLiteOpenHelper(Context context) {
        super(context, "User.db", null, 1);
    }

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table users(id integer primary key autoincrement," +
            "_id text," +
            "num text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

Userdao

private MySQLiteOpenHelper sqLiteOpenHelper;
    private SQLiteDatabase database;

public UserDao(Context context){
    sqLiteOpenHelper=new MySQLiteOpenHelper(context);
    database=sqLiteOpenHelper.getReadableDatabase();
}

public void add(String _id,String num){
    ContentValues values = new ContentValues();
    values.put("num",num);
    values.put("_id",_id);
    database.insert("users",null,values);
}
public void delAll(){
    database.delete("users",null,null);
}

public void del(String _id){
    database.delete("users","_id=?",new String[]{_id});
}
public List<UserBean> select(){
    ArrayList<UserBean> list = new ArrayList<>();
    Cursor cursor = database.query("users", null, null, null, null, null, null);
    while (cursor.moveToNext()){
        String num = cursor.getString(cursor.getColumnIndex("num"));
        String _id = cursor.getString(cursor.getColumnIndex("_id"));
        UserBean userBean=new UserBean(num,_id);
        list.add(userBean);
    }
    return list;
}

edit_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke android:width="1px"/>
    <corners android:radius="4dp"/>
    <solid android:color="#fff"/>


</shape>

tiitle布局

<ImageView
        android:id="@+id/imageView"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/search"
        />
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入字体"
        />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值