根據(jù)扔物線大佬教程重寫的TableLayout

需求及前情提要

現(xiàn)有需求插佛,按總寬度N等分辟癌,從左往右放置子布局背苦,至最右處換下行繼續(xù)歼郭,整體布局高度按照子布局高度逐級(jí)擴(kuò)張遗契。

實(shí)際可用效果如圖:


demo.png

之前有做一個(gè)版本 TableLayout 整齊堆疊寬度等分布局 但實(shí)際實(shí)現(xiàn)方式存在問題,先根據(jù)扔物線大佬的教程視頻重做一版如下:

/**
 * TableLayout 2.0
 * Author: @Kongzue
 * Github: https://github.com/kongzue/
 * Homepage: http://kongzue.com/
 * Mail: myzcxhh@live.cn
 * CreateTime: 2019/4/24 15:51
 */
public class TableLayout extends ViewGroup {
    
    private int maxColumn = 2;
    private int itemMargin = 0;
    private int setChildHeight = dp2px(150);
    
    private Context context;
    
    public TableLayout(Context context) {
        super(context);
        this.context = context;
        loadAttrs(context, null);
    }
    
    public TableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        
        loadAttrs(context, attrs);
    }
    
    public TableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        
        loadAttrs(context, attrs);
    }
    
    private void loadAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TableLayout);
        
        itemMargin = typedArray.getDimensionPixelOffset(R.styleable.TableLayout_marginDp, 0);
        setChildHeight = typedArray.getDimensionPixelOffset(R.styleable.TableLayout_itemHeight, dp2px(150));
        maxColumn = typedArray.getInteger(R.styleable.TableLayout_column, 2);
        
        typedArray.recycle();
    }
    
    private List<View> items;
    private int newHeight = 0;
    
    private int[] childTop, childLeft, childRight, childBottom;
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxWidth = resolveSize(Integer.MAX_VALUE, widthMeasureSpec) + itemMargin;
        int itemWidth = maxWidth / maxColumn - itemMargin;
        int itemHeight = resolveSize(setChildHeight, heightMeasureSpec);
        
        childTop = new int[getChildCount()];
        childLeft = new int[getChildCount()];
        childRight = new int[getChildCount()];
        childBottom = new int[getChildCount()];
        
        int l = 0, t = 0, r = 0, b = 0;
        
        items = new ArrayList<>();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == VISIBLE) {
                items.add(getChildAt(i));
            }
        }
        
        //調(diào)用每個(gè)子view的onMeasure測(cè)量子view
        for (int i = 0; i < items.size(); i++) {
            View child = items.get(i);
            //LayoutParams lp = child.getLayoutParams();
            
            int childWidthSpec = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
            int childHeightSpec = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
            
            if (i != 0 && i % maxColumn == 0) {
                l = 0;
                t = t + itemHeight + itemMargin;
            }
            r = l + itemWidth;
            b = t + itemHeight;
            
            childLeft[i] = l;
            childTop[i] = t;
            childRight[i] = r;
            childBottom[i] = b;
            
            l = l + itemWidth + itemMargin;
            newHeight = t + itemHeight;
            
            child.measure(childWidthSpec, childHeightSpec);
        }
        
        int mWidth = resolveSize(itemWidth, widthMeasureSpec);
        int mHeight = resolveSize(itemHeight, heightMeasureSpec);
        
        //setMeasuredDimension(mWidth, mHeight);
        
        //refreshViews(widthMeasureSpec, heightMeasureSpec);
        
        setMeasuredDimension(mWidth, newHeight);//設(shè)置寬高
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < items.size(); i++) {
            View child = items.get(i);
            child.layout(childLeft[i], childTop[i], childRight[i], childBottom[i]);
        }
    }
    
    @Override
    public void addView(View child) {
        super.addView(child);
    }
    
    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
    }
    
    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
    }
    
    @Override
    public void addView(View child, LayoutParams params) {
        super.addView(child, params);
    }
    
    @Override
    public void addView(View child, int index, LayoutParams params) {
        super.addView(child, index, params);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
    
    private int dp2px(float dpValue) {
        return (int) (0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density);
    }
    
    public int getMaxColumn() {
        return maxColumn;
    }
    
    public TableLayout setMaxColumn(int maxColumn) {
        this.maxColumn = maxColumn;
        return this;
    }
    
    public int getItemMargin() {
        return itemMargin;
    }
    
    public TableLayout setItemMargin(int itemMargin) {
        this.itemMargin = itemMargin;
        return this;
    }
    
    public int getItemHeight() {
        return setChildHeight;
    }
    
    public TableLayout setItemHeight(int itemHeight) {
        this.setChildHeight = itemHeight;
        return this;
    }
}

附錄

視頻鏈接:
扔物線大佬的《定制 Layout 的內(nèi)部布局》:
https://www.bilibili.com/video/BV1QW411t7op
https://www.bilibili.com/video/BV1CW411q77t

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末实撒,一起剝皮案震驚了整個(gè)濱河市姊途,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌知态,老刑警劉巖捷兰,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異负敏,居然都是意外死亡贡茅,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門其做,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)顶考,“玉大人,你說(shuō)我怎么就攤上這事妖泄【匝兀” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵蹈胡,是天一觀的道長(zhǎng)渊季。 經(jīng)常有香客問我朋蔫,道長(zhǎng),這世上最難降的妖魔是什么却汉? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任驯妄,我火速辦了婚禮,結(jié)果婚禮上合砂,老公的妹妹穿的比我還像新娘青扔。我一直安慰自己,他們只是感情好翩伪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布微猖。 她就那樣靜靜地躺著,像睡著了一般缘屹。 火紅的嫁衣襯著肌膚如雪励两。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天囊颅,我揣著相機(jī)與錄音,去河邊找鬼傅瞻。 笑死踢代,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的嗅骄。 我是一名探鬼主播胳挎,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼溺森!你這毒婦竟也來(lái)了慕爬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤屏积,失蹤者是張志新(化名)和其女友劉穎医窿,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炊林,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡姥卢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了渣聚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片独榴。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖奕枝,靈堂內(nèi)的尸體忽然破棺而出棺榔,到底是詐尸還是另有隱情,我是刑警寧澤隘道,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布症歇,位于F島的核電站郎笆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏当船。R本人自食惡果不足惜题画,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望德频。 院中可真熱鬧苍息,春花似錦、人聲如沸壹置。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)钞护。三九已至盖喷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間难咕,已是汗流浹背课梳。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留余佃,地道東北人暮刃。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像爆土,于是被迫代替她去往敵國(guó)和親椭懊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容