自定义View实现搜索历史记录

//搜索布局





  

//HeadView类

 

public class HeadView extends LinearLayout {

    private Button backto;
    private Button serch;
    private EditText edtext;

    public HeadView(Context context) {
        super(context);
        info(context);

    }

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

    public HeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        info(context);
    }

    private void info(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.headview, null);
        backto = inflate.findViewById(R.id.backto);
        serch = inflate.findViewById(R.id.serach);
        edtext = inflate.findViewById(R.id.editext);
        serch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = edtext.getText().toString();
                dataCallBack.getdata(s);
            }
        });
    }
    DataCallBack dataCallBack;
    public void  SetDataCallBack(DataCallBack dataCallBack){
        this.dataCallBack=dataCallBack;

    }

    public  interface  DataCallBack{
        void getdata(String data);

    }

//实现添加View

public class serchView extends ViewGroup {
    Context context;
    private int maxHeight;
    private int marginleft = 20;
    private int margintop = 20;
    public serchView(Context context) {
        super(context);
    }
    public serchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public serchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        //找到最大高度方法
        findMaxHeight();
        //定义左边距
        int left = 0;
        //定义上边距
        int top = 0;
        //获取子View的个数
        int childCount = getChildCount();
        //循环最大的子View的个数
        for (int j = 0; j width){
                    //换行 将上边距加上最大高度,让子View高度变高,来实现换行
                    top = top+maxHeight+margintop;
                    left = 0;
                }
            }
            //不超过则把左边距加上子view的宽度即可,bottom是上边距加上子view的高度
            childAt.layout(left,top,left+measuredWidth,top+maxHeight);
            //每次出现一个子View,大左边距都需要变化,将左边距加上子View宽度和
            left = left+measuredWidth+marginleft;
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量父view的宽度
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        //测量MaxHeight的值
        findMaxHeight();
        int left = 0;
        int top = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            int measuredWidth = view.getMeasuredWidth();
            //如果为第一个则不用增加下边距,加左边距即可
            if(left != 0){
                //如果ziview加上左边距大于父View的宽度则换行
                if((left+measuredWidth)>widthsize){
                    //让高度变高,即可能实现换行,下边距加上ziview高度即可
                    //并把左边距赋值为0,从左边开始添加子View
                    top = top+maxHeight+margintop;
                    left = 0;
                }
            }
            left = left+measuredWidth+marginleft;
        }
        //想要实现自己View的宽高大小,必须实现这个方法
        //宽度不用变,高度需要改变,高度是根据添加的行数高度来改变
        heightsize=top+maxHeight;
        setMeasuredDimension(widthsize,heightsize);
    }
    private void findMaxHeight(){
        maxHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            //获取子View的高度
            int measuredHeight = childAt.getMeasuredHeight();
            //如果变量子View最大高度小于子View的高度,
            // 则将子View高度赋值给MaxHeight
            if(maxHeight 
  

//activity中实现

setContentView(R.layout.activity_serch);
headView = findViewById(R.id.headview);
serch = findViewById(R.id.serch);
//添加搜索的内容
headView.setCallBack(new HeadView.EditCallBack() {
    @Override
    public void editcallback(String name) {
        TextView textView = new TextView(SerchActivity.this);
        textView.setText(name);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(20);
        textView.setBackgroundColor(Color.BLACK);
        serch.addView(textView);
    }
});

 

你可能感兴趣的:(自定义View实现搜索历史记录)