Custom search box plus flow layout

The shape of the flow layout

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

    <solid android:color="#666666" />
    <corners android:radius="10dp" />
    <padding
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp"
        />
</shape>

The main content of the flow layout

public class XCFlowLayout extends ViewGroup {
    //存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    //每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();

    public XCFlowLayout(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }
    public XCFlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }
    public XCFlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub

        //父控件传进来的宽度和高度以及对应的测量模式
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //如果当前ViewGroup的宽高为wrap_content的情况
        int width = 0;//自己测量的 宽度
        int height = 0;//自己测量的高度
        //记录每一行的宽度和高度
        int lineWidth = 0;
        int lineHeight = 0;

        //获取子view的个数
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            //测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //子View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //子View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //换行时候
            if(lineWidth + childWidth > sizeWidth){
                //对比得到最大的宽度
                width = Math.max(width, lineWidth);
                //重置lineWidth
                lineWidth = childWidth;
                //记录行高
                height += lineHeight;
                lineHeight = childHeight;
            }else{//不换行情况
                //叠加行宽
                lineWidth += childWidth;
                //得到最大行高
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //处理最后一个子View的情况
            if(i == childCount -1){
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        //wrap_content
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        mAllChildViews.clear();
        mLineHeight.clear();
        //获取当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;
        //记录当前行的view
        List<View> lineViews = new ArrayList<View>();
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            //如果需要换行
            if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){
                //记录LineHeight
                mLineHeight.add(lineHeight);
                //记录当前行的Views
                mAllChildViews.add(lineViews);
                //重置行的宽高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                //重置view的集合
                lineViews = new ArrayList();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);

        //设置子View的位置
        int left = 0;
        int top = 0;
        //获取行数
        int lineCount = mAllChildViews.size();
        for(int i = 0; i < lineCount; i ++){
            //当前行的views和高度
            lineViews = mAllChildViews.get(i);
            lineHeight = mLineHeight.get(i);
            for(int j = 0; j < lineViews.size(); j ++){
                View child = lineViews.get(j);
                //判断是否显示
                if(child.getVisibility() == View.GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                //进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 0;
            top += lineHeight;
        }

    }
    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub

        return new MarginLayoutParams(getContext(), attrs);
    }
}

Main MainActivity

public class MainActivity extends AppCompatActivity {

    private String mNames[] = {
            "应急启动电源", "餐桌", "粽子散装",
            "智能手表", "摩托车配件", "批发方便面",
            "王中王火腿", "手机", "桶装矿泉水",
            "U盘64G", "机械革命电脑", "洗发水",
            "护发素", "奶粉", "search", "logcat"
    };
    private XCFlowLayout mFlowLayout;
    private EditText ed_text;
    private ListView list_view;
    private Button delall;
    private List<String> list;
    private TextView te_sou;
    private MylistAdapter mylistAdapter;

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

        list = new ArrayList<>();
        ed_text = findViewById(R.id.ed_text);
        list_view = findViewById(R.id.list_view);
        delall = findViewById(R.id.delAll);
        te_sou = findViewById(R.id.te_sou);

        initChildViews();
        xiTiao();
        //获取焦点显示光标
        ed_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ed_text.setCursorVisible(true);
            }
        });

        //清空数据并且隐藏
        delall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.clear();
                mylistAdapter.notifyDataSetChanged();
                delall.setVisibility(View.GONE);
            }
        });

    }

    //流式布局
    public void initChildViews() {
        // TODO Auto-generated method stub
        mFlowLayout = findViewById(R.id.liu_shi);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for(int i = 0; i < mNames.length; i ++){
            TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.WHITE);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
            mFlowLayout.addView(view,lp);
        }
    }

    public void xiTiao() {

        //点击搜索并且显示按钮
        te_sou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String shu = ed_text.getText().toString();
                list.add(shu);

                mylistAdapter = new MylistAdapter(MainActivity.this,list);
                list_view.setAdapter(mylistAdapter);

                Intent it = new Intent(MainActivity.this,ZhanActivity.class);
                startActivity(it);

                delall.setVisibility(View.VISIBLE);
            }
        });
    }
}

Main MainActivity layout

<RelativeLayout >
    <LinearLayout
        android:id="@+id/liner"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <ImageView
            android:src="@drawable/leftjiantou"
            android:layout_marginTop="6dp"
            android:layout_width="40dp"
            android:layout_height="40dp" />
        <LinearLayout
            android:layout_marginTop="8dp"
            android:background="@drawable/sousuo_kuang"
            android:orientation="horizontal"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="0dp"
            android:layout_height="40dp">

            <ImageView
                android:layout_width="25dp"
                android:src="@drawable/a_4"
                android:layout_margin="7dp"
                android:layout_gravity="center_vertical"
                android:layout_height="25dp" />
            <EditText
                android:id="@+id/ed_text"
                android:hint="内衣夸店3免1,服装夸店3件7折"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:background="@null"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <ImageView
                android:src="@drawable/root"
                android:layout_margin="5dp"
                android:layout_width="28dp"
                android:layout_height="28dp" />
        </LinearLayout>
        <TextView
            android:id="@+id/te_sou"
            android:text="搜索"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="12dp"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/tere"
        android:text="热搜"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_below="@+id/liner"
        android:layout_margin="7dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.weekone.XCFlowLayout
        android:id="@+id/liu_shi"
        android:layout_below="@+id/tere"
        android:layout_width="match_parent"
        android:layout_height="150dp">

    </com.example.weekone.XCFlowLayout>

    <TextView
        android:id="@+id/te_history"
        android:visibility="gone"
        android:layout_below="@+id/liu_shi"
        android:layout_marginTop="10dp"
        android:text="历史搜索"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_view"
        android:layout_below="@+id/te_history"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    <Button
        android:id="@+id/delAll"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="清空历史搜索"/>

</RelativeLayout>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325237241&siteId=291194637