自定义view——瀑布流

//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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

  <com.example.weeklx01.TitleBar
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
  <com.example.weeklx01.Kojian
      android:id="@+id/lishi"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="搜索历史"
      />
  <com.example.weeklx01.MyView
      android:id="@+id/myview"
      android:layout_marginTop="20dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
  <com.example.weeklx01.Kojian
      android:id="@+id/remen"
      android:layout_marginTop="20dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="热门搜索"
      />
  <com.example.weeklx01.MyView
      android:id="@+id/myview1"
      android:layout_marginTop="20dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
</LinearLayout>

//titie.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/image"
    android:background="@drawable/search"
    android:layout_width="50dp"
    android:layout_height="50dp" />
   <EditText
       android:id="@+id/edit"
       android:layout_width="330dp"
       android:layout_height="wrap_content" />
</LinearLayout>

//Title接口

public class TitleBar extends LinearLayout {
    private ImageView imageView;
    private EditText editText;
    Context context;
    public TitleBar(Context context) {
        super(context);
        this.context=context;
        init();
    }

public TitleBar(Context context,AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
    init();
}

private void init() {
    View view=View.inflate(context,R.layout.activity_titlebar,null);
    imageView=view.findViewById(R.id.image);
    editText=view.findViewById(R.id.edit);
    imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          if (msetButton!=null){
              msetButton.setsuccess(editText.getText().toString());
          }
        }
    });
      addView(view);

}

SetButton msetButton;

public void setBut(SetButton setButton){
    msetButton=setButton;
}


public interface SetButton{
    void setsuccess(String str);
    }
}

//MAIN

public class MainActivity extends AppCompatActivity {
    private MyView myView,myview1;
    private TitleBar titleBar;
    private Dao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inif();


    }

private void inif() {
    //获取资源id
    myView=findViewById(R.id.myview);
    titleBar=findViewById(R.id.title);
    myview1=findViewById(R.id.myview1);
    //实例化dao
    dao=new Dao(MainActivity.this);
    //调用dao查询
    final List<Bean> select = dao.select();
    for (int i = 0; i < select.size(); i++) {
        TextView text=new TextView(MainActivity.this);
        //进来查询
        final int index = i;
        String name = select.get(i).getName();
        text.setTextColor(Color.YELLOW);
        text.setBackgroundResource(R.drawable.deit);
        text.setText(name);
        myView.addView(text);
        //点击删除
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dao.delete(select.get(index).getUuid());
                myView.removeView(v);
            }
        });

    }
    //调用接口方法
    titleBar.setBut(new TitleBar.SetButton() {
        @Override
        public void setsuccess(String str) {
            //用uuid删除
            final UUID uuid = UUID.randomUUID();
            TextView tv=new TextView(MainActivity.this);
            tv.setTextColor(Color.GREEN);
            tv.setText(str);
            tv.setBackgroundResource(R.drawable.deit);
            tv.setTag(uuid);
            myView.addView(tv);
            //数据库添加
            dao.add(str,uuid.toString());
             //点击删除
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uuid = String.valueOf(v.getTag());
                    dao.delete(uuid);
                    myView.removeView(v);
                }
            });
        }
    });
    //热门搜索
    for (int i = 0; i < 20; i++) {
        TextView textView=new TextView(MainActivity.this);
        textView.setText("数量"+i);
        textView.setTextColor(Color.RED);
        textView.setBackgroundResource(R.drawable.deit);
        myview1.addView(textView);
    }
}
}

//控件

@SuppressLint("AppCompatCustomView")
public class Kojian extends TextView {


public Kojian(Context context) {
    super(context);
}

public Kojian(Context context,AttributeSet attrs) {
    super(context, attrs);
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Kojian);
    int color = typedArray.getColor(R.styleable.Kojian_TextColor, Color.RED);
    setTextColor(color);
    //回收
    typedArray.recycle();

}
}

//attr.xml

<resources>
    <declare-styleable name="Kojian">
        <attr name="TextColor" format="color"></attr>
        <attr name="TextName" format="string"></attr>
    </declare-styleable>
</resources>

//

public class MyView extends LinearLayout {

		//最高的孩子
	   private int mMax=20;
	   //间隔
	   private int mJj=20;	

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

    int w = MeasureSpec.getSize(widthMeasureSpec);
    int h = MeasureSpec.getSize(heightMeasureSpec);
    measureChildren(widthMeasureSpec,heightMeasureSpec);
    findH();
    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())>w){
                top+=mMax+mJj;
                left=0;
            }

        }
        left+=view.getMeasuredWidth()+mJj;

    }
    setMeasuredDimension(w,(top+mMax)>h?h:top+mMax);

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    findH();
    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+=mMax+mJj;
                left=0;
            }
        }
        view.layout(left,top,left+view.getMeasuredWidth(),top+mMax);
        left+=view.getMeasuredWidth()+mJj;
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

private void findH() {
    mMax=0;
    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View view = getChildAt(i);
        if (view.getMeasuredHeight()>mMax){
            mMax=view.getMeasuredHeight();
        }
    }
}
}

//数据库

public class Dao {
    MySqilte mySqilte;
    SQLiteDatabase database;
    public Dao(Context context){
        mySqilte=new MySqilte(context);
        database=mySqilte.getReadableDatabase();
    }
    public void add(String name,String uuid){
        ContentValues values=new ContentValues();
        values.put("name",name);
        values.put("uuid",uuid);
        database.insert("user",null,values);
    }
   public List<Bean> select(){
        List<Bean> list=new ArrayList<>();
       Cursor query = database.query("user", null, null, null, null, null, null);
       while (query.moveToNext()){
           String name = query.getString(query.getColumnIndex("name"));
           String uuid = query.getString(query.getColumnIndex("uuid"));
           Bean bean=new Bean(name,uuid);
           list.add(bean);
       }
       return list;
   }

    public void delete(String uuid){
        database.delete("user","uuid=?",new String[]{uuid});
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值