熱門搜索不規則Textview FlowLayout

<com.autohome.vendor.view.HotSearchFlowLayout
    android:id="@+id/flowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</com.autohome.vendor.view.HotSearchFlowLayout>

Activity使用

HotSearchFlowLayout flowlayout = (HotSearchFlowLayout) findViewById(R.id.flowlayout);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int hot_search_pad_5 =  VendorApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.hot_search_pad_5);
lp.leftMargin = hot_search_pad_5;
lp.rightMargin = hot_search_pad_5;
lp.topMargin = hot_search_pad_5;
lp.bottomMargin = hot_search_pad_5;
TextView view = new TextView(InformationArticleSearchActivity.this);
view.setText(hotSearchName);
int padding7 =  VendorApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.hot_search_pad_7);
int padding2=  VendorApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.hot_search_pad_2);
 view.setPadding(padding7, padding2, padding7, padding2);
 view.setTextColor(VendorApplication.getInstance().getResources().getColor(R.color.grey_light));
 view.setBackgroundDrawable(VendorApplication.getInstance().getResources().getDrawable(R.drawable.textview_corners_black_bg));
flowlayout.addView(view, lp);

/**
 * 熱門搜索view
 * Created by jiangbing on 2016/1/4.
 */
public class HotSearchFlowLayout extends ViewGroup {
    private static final String TAG = "FlowLayout";


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

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(
            ViewGroup.LayoutParams p)
    {
        return new MarginLayoutParams(p);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams()
    {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    /**
     * 負責設置子控件的測量模式和大小 根據所有子控件設置自己的寬和高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 獲得它的父容器爲它設置的測量模式和大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        Log.e(TAG, sizeWidth + "," + sizeHeight);

        // 如果是warp_content情況下,記錄寬和高
        int width = 0;
        int height = 0;
        /**
         * 記錄每一行的寬度,width不斷取最大寬度
         */
        int lineWidth = 0;
        /**
         * 每一行的高度,累加至height
         */
        int lineHeight = 0;

        int cCount = getChildCount();

        // 遍歷每個子元素
        for (int i = 0; i < cCount; i++)
        {
            View child = getChildAt(i);
            // 測量每一個child的寬和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到childlp
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
            // 當前子空間實際佔據的寬度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            // 當前子空間實際佔據的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;
            /**
             * 如果加入當前child,則超出最大寬度,則的到目前最大寬度給width,類加height 然後開啓新行
             */
            if (lineWidth + childWidth > sizeWidth)
            {
                width = Math.max(lineWidth, childWidth);// 取最大的
                lineWidth = childWidth; // 重新開啓新行,開始記錄
                // 疊加當前高度,
                height += lineHeight;
                // 開啓記錄下一行的高度
                lineHeight = childHeight;
            } else
            // 否則累加值lineWidth,lineHeight取最大高度
            {
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 如果是最後一個,則將當前記錄的最大寬度和當前lineWidth做比較
            if (i == cCount - 1)
            {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }

        }
        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
                : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
                : height);

    }
    /**
     * 存儲所有的View,按行記錄
     */
    private List<List<View>> mAllViews = new ArrayList<List<View>>();
    /**
     * 記錄每一行的最大高度
     */
    private List<Integer> mLineHeight = new ArrayList<Integer>();
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        mAllViews.clear();
        mLineHeight.clear();

        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;
        // 存儲每一行所有的childView
        List<View> lineViews = new ArrayList<View>();
        int cCount = getChildCount();
        // 遍歷所有的孩子
        for (int i = 0; i < cCount; i++)
        {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            // 如果已經需要換行
            if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width)
            {
                // 記錄這一行所有的View以及最大高度
                mLineHeight.add(lineHeight);
                // 將當前行的childView保存,然後開啓新的ArrayList保存下一行的childView
                mAllViews.add(lineViews);
                lineWidth = 0;// 重置行寬
                lineViews = new ArrayList<View>();
            }
            /**
             * 如果不需要換行,則累加
             */
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
                    + lp.bottomMargin);
            lineViews.add(child);
        }
        // 記錄最後一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);

        int left = 0;
        int top = 0;
        // 得到總行數
        int lineNums = mAllViews.size();
        for (int i = 0; i < lineNums; i++)
        {
            // 每一行的所有的views
            lineViews = mAllViews.get(i);
            // 當前行的最大高度
            lineHeight = mLineHeight.get(i);

            Log.e(TAG, "" + i + "行 :" + lineViews.size() + " , " + lineViews);
            Log.e(TAG, "" + i + "行, :" + lineHeight);

            // 遍歷當前行所有的View
            for (int j = 0; j < lineViews.size(); j++)
            {
                View child = lineViews.get(j);
                if (child.getVisibility() == View.GONE)
                {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child
                        .getLayoutParams();

                //計算childViewleft,top,right,bottom
                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc =lc + child.getMeasuredWidth();
                int bc = tc + child.getMeasuredHeight();

                Log.e(TAG, child + " , l = " + lc + " , t = " + t + " , r ="
                        + rc + " , b = " + bc);

                child.layout(lc, tc, rc, bc);

                left += child.getMeasuredWidth() + lp.rightMargin
                        + lp.leftMargin;
            }
            left = 0;
            top += lineHeight;
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章