Android标签、热门搜索实现支持横纵排列

1.定义ShowButtonLayout extends ViewGroup,重写onMeasure方法

/** * 测量宽度和高度 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //获取流式布局的宽度和模式 int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); //获取流式布局的高度和模式 int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    //使用wrap_content的流式布局的最终宽度和高度
    int width = 0, height = 0;
    //记录每一行的宽度和高度
    int lineWidth = 0, lineHeight = 0;
    //得到内部元素的个数
    int count = getChildCount();
    mChildPos.clear();
    for (int i = 0; i < count; i++) {
        //获取对应索引的view
        View child = getChildAt(i);
        //测量子view的宽和高
        measureChild(child, widthMeasureSpec, heightMeasureSpec);
        MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
        //子view占据的宽度
        int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
        //子view占据的高度
        int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
        //换行
        if (lineWidth + childWidth > widthSize - getPaddingLeft() - getPaddingRight()) {
            //取最大的行宽为流式布局宽度
            width = Math.max(width, lineWidth);
            //叠加行高得到流式布局高度
            height += lineHeight;
            //重置行宽度为第一个View的宽度
            lineWidth = childWidth;
            //重置行高度为第一个View的高度
            lineHeight = childHeight;
            //记录位置
            mChildPos.add(new ChildPos(
                    getPaddingLeft() + lp.leftMargin,
                    getPaddingTop() + height + lp.topMargin,
                    getPaddingLeft() + childWidth - lp.rightMargin,
                    getPaddingTop() + height + childHeight - lp.bottomMargin));
        } else {  //不换行
            //记录位置
            mChildPos.add(new ChildPos(
                    getPaddingLeft() + lineWidth + lp.leftMargin,
                    getPaddingTop() + height + lp.topMargin,
                    getPaddingLeft() + lineWidth + childWidth - lp.rightMargin,
                    getPaddingTop() + height + childHeight - lp.bottomMargin));
            //叠加子View宽度得到新行宽度
            lineWidth += childWidth;
            //取当前行子View最大高度作为行高度
            lineHeight = Math.max(lineHeight, childHeight);
        }
        //最后一个控件
        if (i == count - 1)
        {
            width = Math.max(lineWidth, width);
            height += lineHeight;
        }
    }

    setMeasuredDimension(
            widthMode == MeasureSpec.EXACTLY ? widthSize : width + getPaddingLeft() + getPaddingRight(),
            heightMode == MeasureSpec.EXACTLY ? heightSize : height + getPaddingTop() + getPaddingBottom());
}
复制代码
2.在xml文件中引用
<com.xsy.lib.ShowButtonLayout
    android:id="@+id/mShowBtnLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
复制代码
3.设置数据

for (int i = 0; i < hotWords.length; i++) { TextView view = (TextView) LayoutInflater.from(this).inflate(R.layout.hot_search_tv, mShowBtnLayout, false); view.setText(hotWords[i]); view.setTag(hotWords[i]); view.setOnClickListener(new View.OnClickListener() {//设置点击事件 @Override public void onClick(View view) { String keyword = (String) view.getTag(); Toast.makeText(MainActivity.this,keyword,Toast.LENGTH_LONG).show(); } }); mShowBtnLayout.addView(view);//添加到该view中 }

4.效果图

5.github地址

ShowButtonLayout

转载于:https://juejin.im/post/5a6db39e518825734f52f3c1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值