Android自定義佈局系列之一:流式佈局(含TextView的點選事件)
阿新 • • 發佈:2019-02-02
前言:
之前寫了Unity優化方面的文章,之後就沒寫了。之後想把C盤擴大點,室友試了分割槽助手,很好用,也成功了,我心動也試了下,以為不會出什麼意外,更不會出現資料丟失,抱著僥倖的心理沒臨時備份,哎!沒想到最重要的E盤(所有資料)裡所有資料都丟失了,之後用了資料恢復,臥槽,等資料全找回了,居然說要註冊費100元,真的氣死我了!!只好還原一年前的資料備份了,一場血淋淋的教訓啊!
進入主題:
在網上也找到了一些關於流失佈局的好文章,但是好像沒有新增點選事件,所以我在他們的基礎上完善了下,添加了點選事件,接下來我將會貼出核心的原始碼!
MainActivity:
CoustomFlowLayout:<span style="color:#330033;">package com.ysj.flowlayout; import java.util.LinkedList; import java.util.List; import java.util.Random; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private String mNames[] = { "考研", "求職", "面試 ", "四六級", "國二", "駕照", "電視劇", "電影", "小說", "美食", "遊戲" }; private int[] background = { R.drawable.blue, R.drawable.green, R.drawable.red, R.drawable.yellow }; private XCFlowLayout mFlowLayout = null; private Context mContext = null; private List<TextView> tvs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { initData(); initChildViews(); initEvent(); } private void initData() { mContext = this; tvs = new LinkedList<TextView>(); } private void initChildViews() { mFlowLayout = (XCFlowLayout) findViewById(R.id.flowlayout); LayoutInflater mInflater = LayoutInflater.from(this); // MarginLayoutParams lp = new MarginLayoutParams( // LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // lp.leftMargin = 5; // lp.rightMargin = 5; // lp.topMargin = 5; // lp.bottomMargin = 5; Random random = new Random(); for (int i = 0; i < mNames.length; i++) { TextView tv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(mNames[i]); tv.setTextColor(Color.WHITE); tv.setTextSize(22); tv.setGravity(0); tv.setBackgroundResource(background[random.nextInt(4)]); tvs.add(tv); mFlowLayout.addView(tv); } } private void initEvent() { tvs.get(0).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "考研", Toast.LENGTH_SHORT).show(); } }); } }</span>
<span style="color:#330033;">package com.ysj.flowlayout; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * * @author ysj * */ public class CoustomFlowLayout extends ViewGroup { // 儲存所有子View private List<List<View>> mAllChildViews = new ArrayList<>(); // 每一行的高度 private List<Integer> mLineHeight = new ArrayList<>(); public CoustomFlowLayout(Context context) { this(context, null); } public CoustomFlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CoustomFlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 父控制元件傳進來的寬度和高度以及對應的測量模式 int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); // 如果當前ViewGroup的寬高為wrap_content的情況 int width = 0;// 自己測量的寬度 int height = 0;// 自己測量的高度 // 記錄每一行的寬度和高度 int lineWidth = 0; int lineHeight = 0; // 獲取子view的個數 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); // 測量子View的寬和高 measureChild(child, widthMeasureSpec, heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams(); int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; // 換行時候 if (lineWidth + childWidth > sizeWidth) { // 對比得到最大的寬度 width = Math.max(width, lineWidth); height += lineHeight; // 重置lineWidth,lineHeight lineWidth = childWidth; lineHeight = childHeight; } else {// 不換行情況 // 疊加行寬 lineWidth += childWidth; // 得到最大行高 lineHeight = Math.max(lineHeight, childHeight); } // 處理最後一個子View的情況 if (i == childCount - 1) { width = Math.max(width, lineWidth); height += lineHeight; } } // wrap_content setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width, modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mAllChildViews.clear(); mLineHeight.clear(); // 獲取當前ViewGroup的寬度 int width = getWidth(); int lineWidth = 0; int lineHeight = 0; // 記錄當前行的view List<View> lineViews = new ArrayList<View>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child .getLayoutParams(); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); // 如果需要換行 if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) { // 記錄LineHeight mLineHeight.add(lineHeight); // 記錄當前行的Views mAllChildViews.add(lineViews); // 重置行的寬高 lineWidth = 0; lineHeight = childHeight + lp.topMargin + lp.bottomMargin; // 重置view的集合 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); mAllChildViews.add(lineViews); // 設定子View的位置 int left = 0; int top = 0; // 獲取行數 int lineCount = mAllChildViews.size(); System.out.println("lineCount:"+lineCount); for (int i = 0; i < lineCount; i++) { // 當前行的views和高度 lineViews = mAllChildViews.get(i); lineHeight = mLineHeight.get(i); for (int j = 0; j < lineViews.size(); j++) { View child = lineViews.get(j); // 判斷是否顯示 if (child.getVisibility() == View.GONE) { continue; } MarginLayoutParams lp = (MarginLayoutParams) child .getLayoutParams(); int cLeft = left + lp.leftMargin; int cTop = top + lp.topMargin; int cRight = cLeft + child.getMeasuredWidth(); int cBottom = cTop + child.getMeasuredHeight(); // 進行子View進行佈局 child.layout(cLeft, cTop, cRight, cBottom); left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } left = 0; top += lineHeight; } } /** * 與當前ViewGroup對應的LayoutParams */ @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } }</span>
原始碼下載地址:http://pan.baidu.com/s/1i38ngwP
最後的最後:
我試了下,給TextView設定id,繼承OnClickListener,貌似實現點選事件時系統會去R檔案裡搜尋ID,可是給TextView設定id沒有寫入R檔案內,所以換了種方法實現,如果大家有更好的實現方法請多多指教哦!