label面板

/**
 * 
 * 用户输入时的联想词展示
 */

public class SuggestLabelView extends LinearLayout implements View.OnClickListener {


    /** 控件总行数 **/
    private int MAX_LINE = 3;
    /**
     * 按钮总行数 *
     */
    private int columns = 0;

    /**
     * 设置子view的宽度外部设定,需要根据子view的宽度布局换行时会用 *
     */
    private int mCellWidth = 0;
    /**
     * 设置子view的高度 *
     */
    private int mCellHeight = 0;
    /**
     * //layout总宽度 *
     */
    int layoutTotalWidth = 0;
    /**
     * 子控件的间距*
     */
    int mLabelPadding = 0;

    /**
     * 夜间模式相关
     */
    private int SUGESSTION_HIGH_LINE_COLOR = Color.parseColor("#14b428");
    private int SUGESSTION_HIGH_HISTROY_COLOR = Color.parseColor("#7777cc");
    private int mSugTextColor;
    private int mSugBg;

    /**
     * 一定时间内屏蔽点击 防止重复点击导致触发多次
     */
    private BtnDisableHandler mDisableBtnHandler;
    private final int EVENT_SEARCH_BTN_RELEASE = 1;
    private boolean mIsSearchEnable = true;

    //搜索回调
    private SearchFloatFragmentController searchFloatFragmentController;

    public SuggestLabelView(Context context) {
        super(context);
        setOnClickListener(this);
        initSize();
    }

    public SuggestLabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        initSize();
    }

    private void initSize() {
        Resources resources = QihooApplication.getInstance().getResources();
        int displayWidth = resources.getDisplayMetrics().widthPixels;
        layoutTotalWidth = displayWidth;
        mLabelPadding = resources.getDimensionPixelSize(R.dimen.float_fragment_label_margin);
        mCellHeight = resources.getDimensionPixelSize(R.dimen.float_fragment_label_height);
        mDisableBtnHandler = new BtnDisableHandler();
    }

    public int getLayoutTotalWidth() {
        return layoutTotalWidth;
    }

    public void setLayoutTotalWidth(int layoutTotalWidth) {
        this.layoutTotalWidth = layoutTotalWidth;
    }

    public int getmCellWidth() {
        return mCellWidth;
    }

    public void setmCellWidth(int mCellWidth) {
        this.mCellWidth = mCellWidth;
    }

    public int getmCellHeight() {
        return mCellHeight;
    }

    public void setmCellHeight(int mCellHeight) {
        this.mCellHeight = mCellHeight;
    }


    public void setLabelTextColor(int color) {
        mSugTextColor = color;
    }

    public void setLabelBackgroundResource(int resid) {
        mSugBg = resid;
    }

    public void setMaxLine(int line){
    	MAX_LINE = line;
    }
    /**
     * 根据LinearLayout总宽度自动匹配布局
     */
    public void autoLayout() {

        int cellWidth = 0;// 子控件宽
        int cellHeight = 0;// 子控件高
        int totalWidth = 0;// 每一行总宽度
//        int lineNum = 1;

        int x = mLabelPadding;
        int y = mLabelPadding;
        int count = getChildCount();
        for (int j = 0; j < count; j++) {
            LogUtils.i("label", "---------------------");
            View childView = getChildAt(j);
            // 获取子控件Child的宽高
            cellWidth = getChildWidth(childView);
            cellHeight = mCellHeight;
            LogUtils.i("label", "autoLayout w=" + cellWidth + ", h=" + cellHeight);

            childView.layout(x, y, x + cellWidth, y + cellHeight);
            LogUtils.i("label", "x before=" + x);
            x += cellWidth + mLabelPadding;
            totalWidth = x;
            LogUtils.i("label", "x after=" + x);
            LogUtils.i("label", "autoLayout totalWidth=" + totalWidth + ", getLayoutTotalWidth=" + getLayoutTotalWidth());
            if (totalWidth > this.getLayoutTotalWidth()) {
                // 换行
                x = mLabelPadding;
                y += cellHeight + mLabelPadding;
//                num++;
                childView.layout(x, y, x + cellWidth, y + cellHeight);
                x += cellWidth + mLabelPadding;
//                if (++lineNum > MAX_LINE)
//                    break;
            } else {
//                x += cellWidth;
//                totalWidth += x;
            }
        }
    }

    /**
     * 获取行数
     * @return
     */
    private int getLineNum() {
        int cellWidth = 0;// 子控件宽
        int cellHeight = 0;// 子控件高
        int totalWidth = 0;// 每一行总宽度
        int num = 1;    //行数

        int x = mLabelPadding;
        int y = 0;
        int count = getChildCount();
        for (int j = 0; j < count; j++) {
            View childView = getChildAt(j);
            // 获取子控件Child的宽高
            cellWidth = getChildWidth(childView);
            cellHeight = mCellHeight;

//            childView.layout(x, y, x + cellWidth, y + cellHeight);
            x += cellWidth + mLabelPadding;
            totalWidth = x;
            if (totalWidth > this.getLayoutTotalWidth()) {
                // 换行
                x = mLabelPadding;
                totalWidth = 0;
                y += cellHeight + mLabelPadding;
                num++;
//                childView.layout(x, y, x + cellWidth, y + cellHeight);
                x += cellWidth + mLabelPadding;
                totalWidth = x;
            } else {
//                x += cellWidth;
//                totalWidth += x;
            }
        }
        return num;
    }


    /**
     * 自动换行模式
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    public void onMeasureByChildContent(int widthMeasureSpec,
                                        int heightMeasureSpec) {

        int count = getChildCount();
        int layoutWidth = 0;
        int childHeight = 0; // 子view的高
//        int a = this.getLayoutParams().width;
//        int b = this.getLayoutParams().height;
//        LogUtils.e("label", "getLayoutParams宽度"+a+", " + "getLayoutParams高度"+ b ) ;
        // 设置子空间Child的宽高
        LogUtils.i("label", "onMeasure count=" + count);
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            layoutWidth += getChildWidth(childView);
            childHeight = mCellHeight;
            LogUtils.i("label", "onMeasure layoutw=" + layoutWidth + ", h=" + childHeight);
        }

        int lineNum = getLineNum();
        lineNum = lineNum > MAX_LINE ? MAX_LINE : lineNum;
        int layoutHeight = count == 0 ? 0 : lineNum * childHeight + (lineNum + 1) * mLabelPadding;

        // 设置容器控件所占区域大小
        setMeasuredDimension(resolveSize(layoutWidth, widthMeasureSpec),
                resolveSize(layoutHeight, heightMeasureSpec));
        LogUtils.i("label", "............onMeasure lineNum=" + lineNum);

        // layoutTotalWidth = this.getWidth() ;
//        LogUtils.i("label", "************************" + layoutTotalWidth);

    }

    /**
     * 控制子控件的换行
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        LogUtils.i("label", "........onLayout");
//        byChildWidthLayout(changed, l, t, r, b);
        autoLayout();
    }

    /**
     * 计算控件及子控件所占区域
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LogUtils.i("label", "........onMeasure");
        onMeasureByChildContent(widthMeasureSpec, heightMeasureSpec);
//        onMeasureByChildWidth(widthMeasureSpec, heightMeasureSpec);
    }

    private int getChildWidth(View childView) {
        /*if (childView.getWidth() != 0) {
            LogUtils.i("label","getChildWidth return width="+childView.getWidth());
            return childView.getWidth();
        }*/
        int cellWidth = 0;
//        int cellHeight = 0;
        TextView textView = (TextView) childView;
        Paint p = textView.getPaint();
        String text = textView.getText().toString();
//        LogUtils.i("label",text);

        Rect rect = new Rect();
        p.getTextBounds(text, 0, text.length(), rect);
        cellWidth = rect.width() + childView.getPaddingLeft() + childView.getPaddingRight();
//        cellHeight = rect.height() + childView.getPaddingTop() + childView.getPaddingBottom();
//        LogUtils.i("label", "getChildWidth w="+cellWidth);
        cellWidth += 10;    //getTextBounds方法有误差 需要加几个像素确保显示完整

        if (cellWidth + mLabelPadding * 2 > layoutTotalWidth) {
            //限制子控件的最大宽度
            cellWidth = layoutTotalWidth - (mLabelPadding * 2 + 10);
        }

        return cellWidth;
    }
    
    //设置数据data接口
	public void setGuessULikeData(ArrayList<LikeItemData> likeData) {
		int childCount = getChildCount();
        int listSize = likeData.size();

        for (int i = 0; i < listSize; i++) {
        	LikeItemData item = likeData.get(i);
        	if(item.getTitle().length() == 1){
        		String s = item.getTitle().replaceAll("[A-Za-z]", "");
            	if(TextUtils.isEmpty(s)){
            		continue;//如果只有一个字母,如 a、  s等,过滤掉
            	}
        	}
            TextView tv;
            if (i < childCount) {
                //更新现有的TextView
                tv = (TextView) getChildAt(i);
            } else {
                tv = new TextView(this.getContext());
                setTextViewParams(tv);
            }
             
                tv.setText(item.getTitle());
                tv.setTextColor(mSugTextColor);
                WidgetUtils.HighLightWords(tv, "", SUGESSTION_HIGH_LINE_COLOR);
            tv.setOnClickListener(this);
            if (i >= childCount) {
                this.addView(tv);
            }
        }

        if (childCount > listSize) {
            this.removeViews(listSize, childCount - listSize);
        }
	}

    /**
     * 设置TextView样式
     * @param tv
     */
    private void setTextViewParams(TextView tv) {
        Resources r = QihooApplication.getInstance().getResources();
        tv.setTextSize(15);
        tv.setHeight(r.getDimensionPixelSize(R.dimen.float_fragment_label_height));
//        tv.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT));
//			tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
        tv.setGravity(Gravity.CENTER_VERTICAL);
//			RelativeLayout.LayoutParams textViewLp = (RelativeLayout.LayoutParams) tv
//					.getLayoutParams();
//			textViewLp.addRule(RelativeLayout.CENTER_VERTICAL);

        //TODO 此TextView的字体不知为何不居中 只能先以padding实现居中
        int paddingLeft = r.getDimensionPixelSize(R.dimen.float_fragment_label_padding_left);
        int paddingTop = r.getDimensionPixelSize(R.dimen.float_fragment_label_padding_top);
        int paddingRight = r.getDimensionPixelSize(R.dimen.float_fragment_label_padding_right);
        int paddingBottom = r.getDimensionPixelSize(R.dimen.float_fragment_label_padding_bottom);
        tv.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
        tv.setBackgroundResource(mSugBg);
        tv.setTextColor(mSugTextColor);
        tv.setSingleLine(true);
    }


    public void setSearchFloatFragmentController(SearchFloatFragmentController controller) {
        this.searchFloatFragmentController = controller;
    }

    @Override
    public void onClick(View v) {
        if (v != null && searchFloatFragmentController != null && v instanceof TextView) {
            if (mIsSearchEnable) {
                String query = ((TextView) v).getText().toString().trim();
                String src = Constant.SRC_INDEX_SUG;
                try{
                	src = TextUtils.isEmpty(searchFloatFragmentController.getQuery()) ? Constant.SRC_INDEX_SUG : Constant.SRC_RESULT_SUG;
                }catch(Exception e){}
                searchFloatFragmentController.doSearch(query, src);
                if (mDisableBtnHandler != null) {
                    mIsSearchEnable = false;
                    mDisableBtnHandler.sendEmptyMessageDelayed(EVENT_SEARCH_BTN_RELEASE, 500);
                }
            }
        }
    }



    private class BtnDisableHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EVENT_SEARCH_BTN_RELEASE:
                    mIsSearchEnable = true;
                    break;
                default:
                    break;
            }
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值