[Android]FlowLayout:流式佈局的應用
阿新 • • 發佈:2018-12-24
一、應用
流式佈局即控制元件根據ViewGroup的寬,自動的往右新增,如果當前行剩餘空間不足,則自動新增到下一行。經常應用於搜尋歷史以及熱搜等介面。
二、實現
1.FlowLayout.java
只要是重寫onMeasure和onLayout兩個函式。
- onMeasure主要是對子控制元件的width和height進行測量
- onLayout主要是對子控制元件進行佈局
public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { super(context, attrs); } public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected LayoutParams generateLayoutParams( LayoutParams mLayoutParams) { return new MarginLayoutParams(mLayoutParams); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected 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); // 如果是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); // 得到child的lp 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(最大寬度) width = Math.max(lineWidth, width); //開始下一行,更新lineWidth lineWidth = childWidth; // 疊加當前高度, height += lineHeight; // 開啟記錄下一行的高度 lineHeight = childHeight; } else { //不需換行,累加lineWidth lineWidth += childWidth; //lineHeight取最大高度 lineHeight = Math.max(lineHeight, childHeight); } // 如果是最後一個,則將當前記錄的最大寬度和當前lineWidth做比較 if (i == cCount - 1) { //防止最後一個的寬度最大,更新width為最大 width = Math.max(width, lineWidth); //保證height最終大於所有lineHeight的累加值 height += lineHeight; } } //建立view //然後根據所有childView的測量得出的寬和高得到該ViewGroup如果設定為wrap_content時的寬和高。 //最後根據模式,如果是MeasureSpec.EXACTLY則直接使用父ViewGroup傳入的寬和高,否則設定為自己計算的寬和高。 setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height); } /** * 儲存所有的View,按行記錄 */ private List<List<View>> mAllViews = new ArrayList<>(); /** * 記錄每一行的最大高度 */ private List<Integer> mLineHeight = new ArrayList<>(); @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<>(); 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<>(); } /** * 如果不需要換行,則累加 */ 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); // 遍歷當前行所有的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(); //計算childView的left,top,right,bottom int lc = left + lp.leftMargin; int tc = top + lp.topMargin; int rc = lc + child.getMeasuredWidth(); int bc = tc + child.getMeasuredHeight(); child.layout(lc, tc, rc, bc); left += child.getMeasuredWidth() + lp.rightMargin + lp.leftMargin; } left = 0; top += lineHeight; } } }
2.history_layout.xml
在佈局中引入FlowLayout控制元件。
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="29.42dp" android:layout_marginStart="29.42dp"> <mysummary.myutils.FlowLayout android:id="@+id/FlowLayout" android:layout_width="wrap_content" android:layout_height="match_parent"> </mysummary.myutils.FlowLayout> </LinearLayout>
3.history_textview_item.xml
設定子控制元件的格式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/history_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:layout_marginRight="12.57dp" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/search_history_text" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="6.13dp" android:layout_marginTop="-4dp" android:ellipsize="end" android:fontFamily="sec-roboto-condensed" android:singleLine="true" android:text="TEXT" android:textColor="@color/colorWhite100" android:textSize="15.94dp" /> </LinearLayout> </LinearLayout>
4.Activity.java
在java檔案中,根據讀取歷史記錄的size,將每條記錄在FlowLayout下顯示出來。
for (int i = 0; i < mHistoryList.size(); i++) {
// 如果root不為null,attachToRoot設為false,則會將佈局檔案最外層的所有layout屬性進行設定
// 當該view被新增到父view當中時,這些layout屬性會自動生效。
View view = LayoutInflater.from(this).inflate(R.layout.view_search_history_item, mFlowLayout, false);
TextView mText = view.findViewById(R.id.search_history_text);
final String text = mHistoryList.get(i);
if (text != null) {
mText.setText(text);
//新增歷史記錄點選事件
//...
mFlowLayout.addView(view);
}
}
以上便是流式佈局FlowLayout的簡單應用以及實現。