100行Android代碼自訂一個流式布局-FlowLayout,android-flowlayout

來源:互聯網
上載者:User

100行Android代碼自訂一個流式布局-FlowLayout,android-flowlayout

首先來看一下 手淘HD - 商品詳情 - 選擇商品屬性 頁面的UI

商品有很多尺碼,而且展現每個尺碼所需要的View的大小也不同(主要是寬度),所以在從伺服器端拉到資料之前,展現所有尺碼所需要的行數和每一行的個數都無法確定,因此不能直接使用GridView或ListView。

如果使用LinearLayout呢?

一個LinearLayout只能顯示一行,如果要展示多行,則每一行都要new一個LinearLayout出來,而且還必須要計算出每一個LinearLayout能容納多少個尺碼對應的View,實現起來也會比較複雜。

其實要實現這個功能,只需要借鑒一下CSS3的flex-box就可以了。

要實現一個Android版本的flexbox,原理非常簡單,為了與Android的命名規範保持一致,我們稱之為FlowLayout。

下面只列出了最核心的程式碼片段,完整代碼已經放到Github上-AndroidFlowLayout,歡迎fork。

在onMeasure中計算FlowLayout的高度
// 遍曆所有的子Viewfor (int i = 0, childCount = getChildCount(); i < childCount; ++i) {    View childView = getChildAt(i);    // measure子View,並擷取它的寬度和高度    LayoutParams childLayoutParams = childView.getLayoutParams();    childView.measure(            getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, childLayoutParams.width),            getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, childLayoutParams.height));    int childWidth = childView.getMeasuredWidth();    int childHeight = childView.getMeasuredHeight();    // 計算當前行的高度(當前行所有子View中最高的那個)    lineHeight = Math.max(childHeight, lineHeight);    // 把當前child view放到上一個child view的右邊,如果放不下,則換行    if (childLeft + childWidth + paddingRight > myWidth) {        childLeft = paddingLeft;        childTop += mVerticalSpacing + lineHeight;        lineHeight = childHeight;    } else {        childLeft += childWidth + mHorizontalSpacing;    }}int wantedHeight = childTop + lineHeight + paddingBottom;// 計算FlowLayout所需要高度setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
在onLayout中對child views進行布局

代碼與onMeasure非常類似,只需要根據child view的寬度和高度放到指定位置即可。

for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {    View childView = getChildAt(i);    if (childView.getVisibility() == View.GONE) {        continue;    }    int childWidth = childView.getMeasuredWidth();    int childHeight = childView.getMeasuredHeight();    lineHeight = Math.max(childHeight, lineHeight);    if (childLeft + childWidth + paddingRight > myWidth) {        childLeft = paddingLeft;        childTop += mVerticalSpacing + lineHeight;        lineHeight = childHeight;    }    // 關鍵代碼    childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);    childLeft += childWidth + mHorizontalSpacing;}

完整版代碼已經放到Github-FlowLayout,打出來的aar包已經上傳到了bintray,使用方式非常簡單,只需要在項目(project)對應的build.gradle中添加一條dependency即可。

compile 'com.liangfeizc:flowlayout:1.0.0@aar'
把aar包上傳到 jCenter

具體做法可參考 publishing gradle android library to jcenter
打包指令碼可參考 flowlayout/build.gradle

有問題請留言。よろしくお願いします

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.