MainActivity

public class MainActivity extends AppCompatActivity {

    private String hot[] = {\"春风十里\", \"体测八百米\", \"海底两万里\", \"德芙巧克力\", \"香草味八喜\",
            \"可可布朗尼\", \"榴莲菠萝蜜\", \"芝士玉米粒\", \"鸡汁土豆泥\", \"黑椒牛里脊\", \"红烧排骨酱里脊\"};
    private HistoryDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        final CustomWeekFlowView fl = findViewById(R.id.fl);

        Custom BarView  Bar = findViewById(R.id. Bar);
         Bar.setmOnButt Listener(new Custom BarView.onButt Listener() {
            @Override
            public void onButt (String str) {
                UUID uuid = UUID.randomUUID();
                TextView text_  = new TextView(MainActivity.this);
                text_ .setTag(uuid);
                text_ .setTextColor(Color.GRAY);
                text_ .setText(str);
                text_ .setBackgroundResource(R.drawable.edit_bg);
                fl.addView(text_ );

                text_ .set Listener(new View. Listener() {
                    @Override
                    public void  (View v) {
                        String uuid = String.valueOf(v.getTag());
                    }
                });
            }
        });

        final CustomHotView hl = findViewById(R.id.hl);
        for (int i = 0; i < hot.length; i++) {
            TextView tv1 = new TextView(MainActivity.this);
            tv1.setText(hot[i]);
            tv1.setTextColor(Color.BLACK);
            hl.addView(tv1);
        }

        dao = new HistoryDao(MainActivity.this);
        HistoryBean bean = new HistoryBean();
        dao.add(bean);
        findViewById(R.id.edit_delall).set Listener(new View. Listener() {
            @Override
            public void  (View v) {
                Toast.makeText(MainActivity.this, \"已清空\", Toast.LENGTH_SHORT).show();

            }
        });
    }
}

头部

public class Custom BarView extends LinearLayout {

    Context mContext;

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

    public Custom BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        View view = View.inflate(mContext, R.layout. barview, null);

        final EditText editText = view.findViewById(R.id.edit_search);

        view.findViewById(R.id.btn_search).set Listener(new  Listener() {
            @Override
            public void  (View v) {
                //6.
                if (mOnButt Listener != null) {
                    //7.
                    mOnButt Listener.onButt (editText.getText().toString());
                }
            }
        });
        addView(view);
    }

    //3.
    onButt Listener mOnButt Listener;

    //4.
    //5.
    public void setmOnButt Listener(onButt Listener OnButt Listener) {
        mOnButt Listener = OnButt Listener;
    }

    //1.
    public interface onButt Listener {
        //2.
        void onButt (String str);
    }
}

搜索历史添加到搜索框下边

public class CustomWeekFlowView extends LinearLayout {

    private int mChildMaxHeight;

    private int mHspace = 20; //左右
    private int mVspace = 20; //上下

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

    public CustomWeekFlowView(Context context, 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);

        //找到最高的孩子的高度
        findMaxChildHeght();

        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  20
                    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);

        findMaxChildHeght();

        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  20
                    top += mChildMaxHeight + mVspace;
                    left = 0;
                }
            }
            view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);
            left += view.getMeasuredWidth() + mHspace;
        }
    }

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

    private void findMaxChildHeght() {
        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 CustomHotView extends LinearLayout {
    private int mChildMaxHeight;
    //左右间距  单位px
    private int mHspace = 20;
    //上下间距
    private int mVSpace = 20;

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

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

    //用Measure方法计算宽高
    @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);

        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 += 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 += mChildMaxHeight + mVSpace;
                    left = 0;
                }
            }
            view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);
            left += view.getMeasuredWidth() + mHspace;
        }
    }

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

    //寻找最高的孩子
    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();
            }
        }
    }
}


BarView

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

    <EditText
        android:layout_width=\"200dp\"
        android:layout_height=\"wrap_content\"
        android:hint=\"请输入搜索内容\"
        android:id=\"@+id/edit_search\"
        android:layout_marginLeft=\"50dp\"
        android:layout_marginTop=\"10dp\"
        android:textColor=\"@color/colorPrimaryDark\"
        />
    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"搜索\"
        android:layout_marginTop=\"10dp\"
        android:id=\"@+id/btn_search\"
        />

</LinearLayout>

activity_main

<?  version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
     ns:tools=\"http://schemas.android.com/tools\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    android:orientation=\"vertical\"
    tools:context=\".MainActivity\">

   <com.bwie.sunli20181130.week_test.Custom BarView
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:id=\"@+id/ Bar\"
        ></com.bwie.sunli20181130.week_test.Custom BarView>
   <LinearLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"40dp\"
       android:orientation=\"horizontal\"
       >
        <TextView
            android:layout_width=\"100dp\"
            android:layout_height=\"match_parent\"
            android:text=\"搜索历史\"
            android:textSize=\"24sp\"
            android:layout_marginLeft=\"10dp\"
            />
       <TextView
           android:layout_width=\"100dp\"
           android:layout_height=\"match_parent\"
           android:text=\"清空历史\"
           android:textSize=\"24sp\"
           android:layout_marginLeft=\"160dp\"
           android:id=\"@+id/edit_delall\"
           android:layout_marginRight=\"10dp\"
           />
    </LinearLayout>
    <com.bwie.sunli20181130.week_test.CustomWeekFlowView
        android:id=\"@+id/fl\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        ></com.bwie.sunli20181130.week_test.CustomWeekFlowView>

    <TextView
        android:layout_width=\"match_parent\"
        android:layout_height=\"40dp\"
        android:text=\"热门搜索\"
        android:textSize=\"24sp\"
        android:layout_marginTop=\"20dp\"
        android:layout_marginLeft=\"10dp\"
        />

    <com.bwie.sunli20181130.week_test.CustomHotView
        android:id=\"@+id/hl\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        ></com.bwie.sunli20181130.week_test.CustomHotView>

</LinearLayout>
收藏 打印