1. 程式人生 > >安卓視線可鎖定首行和首列的表格檢視

安卓視線可鎖定首行和首列的表格檢視

如上圖所示效果,整體檢視構成已標記,聯動效果通過兩個CustomHorizontalScrollView的setOnScrollChangeListener方法繫結實現。


表格裡面資料通過動態新增textview和分割線view繪製而成。

具體看以下程式碼:



package com.rmondjone.locktableview;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import 
android.support.v7.app.AlertDialog; import android.text.Layout; import android.text.StaticLayout; import android.text.TextPaint; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import android.widget.Button; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 說明 可鎖定首行和首列的表格檢視
* 作者 康漢斌 * 建立時間 2017/3/29. */ public class LockTableView implements View.OnClickListener { /** * 上下文 */ private Context mContext; /** * 表格父檢視 */ private ViewGroup mContentView; /** * 表格資料,每一行為一條資料,從表頭計算 */ private ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>(); /** * 表格檢視 */ private View mTableView; /** * 是否鎖定首行 */ private boolean isLockFristRow = true; /** * 是否鎖定首列 */ private boolean isLockFristColumn = true; /** * 最大列寬(dp) */ private int maxColumnWidth; /** * 最小列寬(dp) */ private int minColumnWidth; /** * 最大行高(dp) */ private int maxRowHeight; /** * 最小行高dp) */ private int minRowHeight; /** * 第一行背景顏色 */ private int mFristRowBackGroudColor; /** * 資料為空時的預設值 */ private String mNullableString; /** * 單元格字型大小 */ private int mTextViewSize; /** * 表格頭部字型顏色 */ private int mTableHeadTextColor; /** * 表格內容字型顏色 */ private int mTableContentTextColor; /** * 表格監聽事件 */ private OnTableViewListener mTableViewListener; //表格資料 /** * 表格第一行資料,不包括第一個元素 */ private ArrayList<String> mTableFristData = new ArrayList<>(); /** * 表格第一列資料,不包括第一個元素 */ private ArrayList<String> mTableColumnDatas = new ArrayList<>(); /** * 表格左上角資料 */ private String mColumnTitle; /** * 表格每一行資料,不包括第一行和第一列 */ private ArrayList<ArrayList<String>> mTableRowDatas = new ArrayList<ArrayList<String>>(); /** * 記錄每列最大寬度 */ private ArrayList<Integer> mColumnMaxWidths = new ArrayList<Integer>(); /** * 記錄每行最大高度 */ private ArrayList<Integer> mRowMaxHeights = new ArrayList<Integer>(); /** * 把所有的滾動檢視放圖列表,後面實現聯動效果 */ private ArrayList<HorizontalScrollView> mScrollViews = new ArrayList<HorizontalScrollView>(); //表格檢視 /** * 表格左上角檢視 */ private TextView mColumnTitleView; /** * 第一行佈局(鎖狀態) */ private LinearLayout mLockHeadView; /** * 第一行佈局(未鎖狀態) */ private LinearLayout mUnLockHeadView; /** * 第一行滾動檢視(鎖狀態) */ private CustomHorizontalScrollView mLockScrollView; /** * 第一行滾動檢視(未鎖狀態) */ private CustomHorizontalScrollView mUnLockScrollView; /** * 表格主檢視 */ private ScrollView mTableScrollView; private View lockTableViewContent; private CustomHorizontalScrollView lockScrollViewParent; /** * 構造方法 * * @param mContext 上下文 * @param mContentView 表格父檢視 * @param mTableDatas 表格資料 */ public LockTableView(Context mContext, ViewGroup mContentView, ArrayList<ArrayList<String>> mTableDatas) { this.mContext = mContext; this.mContentView = mContentView; this.mTableDatas = mTableDatas; initAttrs(); } /** * 初始化屬性 */ private void initAttrs() { mTableView = LayoutInflater.from(mContext).inflate(R.layout.locktableview, null); maxColumnWidth = 100; minColumnWidth = 70; minRowHeight=20; maxRowHeight=60; mNullableString = "N/A"; mTableHeadTextColor = R.color.beijin; mTableContentTextColor = R.color.border_color; mFristRowBackGroudColor = R.color.table_head; mTextViewSize = 16; } /** * 展現檢視 */ public void show() { initData(); initView(); mContentView.addView(mTableView); } /** * 初始化表格資料 */ private void initData() { if (mTableDatas != null && mTableDatas.size() > 0) { //檢查資料,如果有一行資料長度不一致,以最長為標準填"N/A"字串,如果有null也替換 int maxLength = 0; for (int i = 0; i < mTableDatas.size(); i++) { if (mTableDatas.get(i).size() >= maxLength) { maxLength = mTableDatas.get(i).size(); } ArrayList<String> rowDatas = mTableDatas.get(i); for (int j = 0; j < rowDatas.size(); j++) { if (rowDatas.get(j) == null || rowDatas.get(j).equals("")) { rowDatas.set(j, mNullableString); } } mTableDatas.set(i, rowDatas); } // Log.e("每行最多個數",maxLength+""); for (int i = 0; i < mTableDatas.size(); i++) { ArrayList<String> rowDatas = mTableDatas.get(i); if (rowDatas.size() < maxLength) { int size=maxLength - rowDatas.size(); for (int j = 0; j <size; j++) { rowDatas.add(mNullableString); } mTableDatas.set(i, rowDatas); } } // //測試 // for (int i=0;i<mTableDatas.size();i++){ // ArrayList<String> rowDatas=mTableDatas.get(i); // StringBuffer b=new StringBuffer(); // for (String str:rowDatas){ // b.append("["+str+"]"); // } // Log.e("第"+i+"行資料",b.toString()+"/"+rowDatas.size()+"個"); // } //初始化每列最大寬度 for (int i = 0; i < mTableDatas.size(); i++) { ArrayList<String> rowDatas = mTableDatas.get(i); StringBuffer buffer = new StringBuffer(); for (int j = 0; j < rowDatas.size(); j++) { TextView textView = new TextView(mContext); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); textView.setText(rowDatas.get(j)); textView.setGravity(Gravity.CENTER); //設定佈局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(45, 45, 45, 45);//android:layout_margin="15dp" textView.setLayoutParams(textViewParams); if (i == 0) { mColumnMaxWidths.add(measureTextWidth(textView, rowDatas.get(j))); buffer.append("[" + measureTextWidth(textView, rowDatas.get(j)) + "]"); } else { int length = mColumnMaxWidths.get(j); int current = measureTextWidth(textView, rowDatas.get(j)); if (current > length) { mColumnMaxWidths.set(j, current); } buffer.append("[" + measureTextWidth(textView, rowDatas.get(j)) + "]"); } } // Log.e("第"+i+"行列最大寬度",buffer.toString()); } // Log.e("每列最大寬度dp:",mColumnMaxWidths.toString()); //初始化每行最大高度 for (int i = 0; i < mTableDatas.size(); i++) { ArrayList<String> rowDatas = mTableDatas.get(i); StringBuffer buffer = new StringBuffer(); TextView textView = new TextView(mContext); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); textView.setGravity(Gravity.CENTER); //設定佈局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(45, 45, 45, 45);//android:layout_margin="15dp" textView.setLayoutParams(textViewParams); int maxHeight=measureTextHeight(textView,rowDatas.get(0)); mRowMaxHeights.add(maxHeight); for (int j = 0; j < rowDatas.size(); j++) { int currentHeight=measureTextHeight(textView,rowDatas.get(j)); buffer.append("["+currentHeight+"]"); if (currentHeight>maxHeight){ mRowMaxHeights.set(i,currentHeight); } } // Log.e("第"+i+"行高度",buffer.toString()); } // Log.e("每行最大高度dp:",mRowMaxHeights.toString()); if (isLockFristRow) { ArrayList<String> fristRowDatas = mTableDatas.get(0); if (isLockFristColumn) { //鎖定第一列 mColumnTitle = fristRowDatas.get(0); fristRowDatas.remove(0); mTableFristData.addAll(fristRowDatas); //構造第一列資料,並且構造表格每行資料 for (int i = 1; i < mTableDatas.size(); i++) { ArrayList<String> rowDatas = mTableDatas.get(i); mTableColumnDatas.add(rowDatas.get(0)); rowDatas.remove(0); mTableRowDatas.add(rowDatas); } } else { mTableFristData.addAll(fristRowDatas); for (int i = 1; i < mTableDatas.size(); i++) { mTableRowDatas.add(mTableDatas.get(i)); } } } else { if (isLockFristColumn) { //鎖定第一列 //構造第一列資料,並且構造表格每行資料 for (int i = 0; i < mTableDatas.size(); i++) { ArrayList<String> rowDatas = mTableDatas.get(i); mTableColumnDatas.add(rowDatas.get(0)); rowDatas.remove(0); mTableRowDatas.add(rowDatas); } } else { for (int i = 0; i < mTableDatas.size(); i++) { mTableRowDatas.add(mTableDatas.get(i)); } } } // Log.e("第一行資料",mTableFristData.toString()); // Log.e("第一列資料",mTableColumnDatas.toString()); // Log.e("每行資料",mTableRowDatas.toString()); } else { Toast.makeText(mContext, "表格資料為空!", Toast.LENGTH_SHORT).show(); } } /** * 初始化表格檢視 */ private void initView() { mColumnTitleView = (TextView) mTableView.findViewById(R.id.lockHeadView_Text); mLockHeadView = (LinearLayout) mTableView.findViewById(R.id.lockHeadView); mUnLockHeadView = (LinearLayout) mTableView.findViewById(R.id.unLockHeadView); mLockScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.lockHeadView_ScrollView); mUnLockScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.unlockHeadView_ScrollView); mTableScrollView = (ScrollView) mTableView.findViewById(R.id.table_scrollView); mLockHeadView.setBackgroundColor(ContextCompat.getColor(mContext, mFristRowBackGroudColor)); mUnLockHeadView.setBackgroundColor(ContextCompat.getColor(mContext, mFristRowBackGroudColor)); if (isLockFristRow) { if (isLockFristColumn) { mLockHeadView.setVisibility(View.VISIBLE); mUnLockHeadView.setVisibility(View.GONE); } else { mLockHeadView.setVisibility(View.GONE); mUnLockHeadView.setVisibility(View.VISIBLE); } creatHeadView(); } else { mLockHeadView.setVisibility(View.GONE); mUnLockHeadView.setVisibility(View.GONE); } createTableView(); } /** * 建立頭部檢視 */ private void creatHeadView() { if (isLockFristColumn) { mColumnTitleView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor)); mColumnTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); mColumnTitleView.setText(mColumnTitle); LinearLayout.LayoutParams layoutParams =(LinearLayout.LayoutParams) mColumnTitleView.getLayoutParams(); layoutParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(0)); layoutParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(0)); layoutParams.setMargins(45, 45, 45, 45); mColumnTitleView.setLayoutParams(layoutParams); //構造滾動檢視 createScollview(mLockScrollView, mTableFristData, true); mScrollViews.add(mLockScrollView); mLockScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() { @Override public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) { changeAllScrollView(x, y); } }); } else { createScollview(mUnLockScrollView, mTableFristData, true); mScrollViews.add(mUnLockScrollView); mUnLockScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() { @Override public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) { changeAllScrollView(x, y); } }); } } /** * 構造表格主檢視 */ private void createTableView() { if (isLockFristColumn) { createLockColumnView(); } else { createUnLockColumnView(); } } /** * 建立鎖定列檢視 */ private void createLockColumnView() { lockTableViewContent=LayoutInflater.from(mContext).inflate(R.layout.locktablecontentview,null); LinearLayout lockViewParent=(LinearLayout) lockTableViewContent.findViewById(R.id.lockView_parent); lockScrollViewParent=(CustomHorizontalScrollView)lockTableViewContent.findViewById(R.id.lockScrollView_parent); //構造鎖定檢視 for (int i=0;i<mTableColumnDatas.size();i++){ //構造TextView容器,如果要設定背景色設定這一個而不是設定TextView,TextView有外邊距設定不全 LinearLayout textViewContainer=new LinearLayout(mContext); LinearLayout.LayoutParams textLinearParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewContainer.setOrientation(LinearLayout.HORIZONTAL); textViewContainer.setLayoutParams(textLinearParams); //構造TextView TextView textView=new TextView(mContext); //設定佈局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(45, 45, 45, 45); textView.setLayoutParams(textViewParams); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); textView.setText(mTableColumnDatas.get(i)); textView.setGravity(Gravity.CENTER); ViewGroup.LayoutParams layoutParams = textView.getLayoutParams(); layoutParams.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(0)); if (isLockFristRow){ layoutParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i+1)); }else{ layoutParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i)); } textView.setLayoutParams(layoutParams); if (!isLockFristRow){ if (i==0){ textViewContainer.setBackgroundColor(ContextCompat.getColor(mContext,mFristRowBackGroudColor)); textView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor)); }else{ textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } }else{ textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } textViewContainer.addView(textView); //表格線 View splite=new View(mContext); ViewGroup.LayoutParams spliteLayoutParam= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext,1)); splite.setLayoutParams(spliteLayoutParam); splite.setBackgroundColor(ContextCompat.getColor(mContext,R.color.light_gray)); lockViewParent.addView(textViewContainer); lockViewParent.addView(splite); } //構造滾動檢視 LinearLayout scollViewItemContentView=new LinearLayout(mContext); LinearLayout.LayoutParams scollViewItemContentViewParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); scollViewItemContentView.setLayoutParams(scollViewItemContentViewParams); scollViewItemContentView.setOrientation(LinearLayout.VERTICAL); for (int i=0;i<mTableRowDatas.size();i++){ final ArrayList<String> datas=mTableRowDatas.get(i); //設定LinearLayout LinearLayout linearLayout = new LinearLayout(mContext); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(layoutParams); linearLayout.setGravity(Gravity.CENTER); linearLayout.setOrientation(LinearLayout.HORIZONTAL); if (!isLockFristRow){ if (i==0){ linearLayout.setBackgroundColor(ContextCompat.getColor(mContext,mFristRowBackGroudColor)); } } for (int j = 0; j < datas.size(); j++) { //構造單元格 TextView textView = new TextView(mContext); if (!isLockFristRow){ if (i==0) { textView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor)); } else { textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } }else{ textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); textView.setGravity(Gravity.CENTER); textView.setText(datas.get(j)); //設定佈局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(45, 45, 45, 45); if (isLockFristRow){ textViewParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i+1)); }else{ textViewParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i)); } textView.setLayoutParams(textViewParams); ViewGroup.LayoutParams textViewParamsCopy = textView.getLayoutParams(); textViewParamsCopy.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(j+1)); linearLayout.addView(textView); //右側分隔線 if (j != datas.size() - 1) { View splitView = new View(mContext); ViewGroup.LayoutParams splitViewParmas = new ViewGroup.LayoutParams(DisplayUtil .dip2px(mContext, 1), ViewGroup.LayoutParams.MATCH_PARENT); splitView.setLayoutParams(splitViewParmas); if (!isLockFristRow){ if (i==0) { splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white)); } else { splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray)); } }else{ splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray)); } linearLayout.addView(splitView); } //加入點選事件 // 加入點選事件 final int finalJ = j; textView.setClickable(true); final int finalI = i; textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("斷案事件", datas.get(finalJ)); String msg = ""; if (finalJ == 0) { msg = mTableColumnDatas.get(finalI)+"下一個資料為" + datas.get(finalJ + 1); } else if (finalJ == datas.size() - 1) { msg = "上個數據為" + datas.get(finalJ - 1); } else { msg = "上個數據為" + datas.get(finalJ - 1) + "\n" + "下一個資料為" + datas.get(finalJ + 1); } new AlertDialog.Builder(mContext) .setTitle("短按提示") .setMessage(msg) .setCancelable(true) .show(); } }); // 加入長按事件 textView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Log.e("長按事件", datas.get(finalJ)); String msg = ""; if (finalJ == 0) { msg = "下一個資料為" + datas.get(finalJ + 1); } else if (finalJ == datas.size() - 1) { msg = "上個數據為" + datas.get(finalJ - 1); } else { msg = "上個數據為" + datas.get(finalJ - 1) + "\n" + "下一個資料為" + datas.get(finalJ + 1); } new AlertDialog.Builder(mContext) .setTitle("短按提示") .setMessage(msg) .setCancelable(true) .show(); return true; } }); } scollViewItemContentView.addView(linearLayout); //底部分隔線 View splite=new View(mContext); ViewGroup.LayoutParams spliteLayoutParam= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext,1)); splite.setLayoutParams(spliteLayoutParam); splite.setBackgroundColor(ContextCompat.getColor(mContext,R.color.light_gray)); scollViewItemContentView.addView(splite); } lockScrollViewParent.addView(scollViewItemContentView); mScrollViews.add(lockScrollViewParent); lockScrollViewParent.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() { @Override public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) { changeAllScrollView(x,y); } }); mTableScrollView.addView(lockTableViewContent); } /** * 建立不鎖定列檢視 */ private void createUnLockColumnView() { lockTableViewContent=LayoutInflater.from(mContext).inflate(R.layout.unlocktablecontentview,null); lockScrollViewParent=(CustomHorizontalScrollView) lockTableViewContent.findViewById(R.id.unlockScrollView_parent); //構造滾動檢視 LinearLayout scollViewItemContentView=new LinearLayout(mContext); LinearLayout.LayoutParams scollViewItemContentViewParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); scollViewItemContentView.setLayoutParams(scollViewItemContentViewParams); scollViewItemContentView.setOrientation(LinearLayout.VERTICAL); for (int i=0;i<mTableRowDatas.size();i++){ ArrayList<String> datas=mTableRowDatas.get(i); //設定LinearLayout LinearLayout linearLayout = new LinearLayout(mContext); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(layoutParams); linearLayout.setGravity(Gravity.CENTER); linearLayout.setOrientation(LinearLayout.HORIZONTAL); if (!isLockFristRow){ if (i==0){ linearLayout.setBackgroundColor(ContextCompat.getColor(mContext,mFristRowBackGroudColor)); } } for (int j = 0; j < datas.size(); j++) { //構造單元格 TextView textView = new TextView(mContext); if (!isLockFristRow){ if (i==0) { textView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor)); } else { textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } }else{ textView.setTextColor(ContextCompat.getColor(mContext, mTableContentTextColor)); } textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize); textView.setGravity(Gravity.CENTER); textView.setText(datas.get(j)); //設定佈局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(45, 45, 45, 45); if (isLockFristRow){ textViewParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i+1)); }else{ textViewParams.height=DisplayUtil.dip2px(mContext,mRowMaxHeights.get(i)); } textView.setLayoutParams(textViewParams); ViewGroup.LayoutParams textViewParamsCopy = textView.getLayoutParams(); textViewParamsCopy.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(j)); linearLayout.addView(textView); //右側分隔線 if (j != datas.size() - 1) { View splitView = new View(mContext); ViewGroup.LayoutParams splitViewParmas = new ViewGroup.LayoutParams(DisplayUtil.dip2px(mContext, 1), ViewGroup.LayoutParams.MATCH_PARENT); splitView.setLayoutParams(splitViewParmas); if (!isLockFristRow){ if (i==0) { splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white)); } else { splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray)); } }else{ splitView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_gray)); } linearLayout.addView(splitView); } } scollViewItemContentView.addView(linearLayout); //底部分隔線 View splite=new View(mContext); ViewGroup.LayoutParams spliteLayoutParam= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext,1)); splite.setLayoutParams(spliteLayoutParam); splite.setBackgroundColor(ContextCompat.getColor(mContext,R.color.light_gray)); scollViewItemContentView.addView(splite); } lockScrollViewParent.addView(scollViewItemContentView); mScrollViews.add(lockScrollViewParent); lockScrollViewParent.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() { @Override public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) { changeAllScrollView(x,y); } }); mTableScrollView.addView(lockTableViewContent); } /** * 改變所有滾動檢視位置 * * @param x * @param y */ private void changeAllScrollView(int x, int y) { if (mScrollViews.size() > 0) { if (mTableViewListener!=null){ mTableViewListener.onTableViewScrollChange(x,y); } for (int i = 0; i < mScrollViews.size(); i++) { HorizontalScrollView scrollView = mScrollViews.get(i); scrollView.scrollTo(x, y); } } } /** * 根據最大最小值,計算TextView的寬度 * * @param textView * @param text * @return */ private int measureTextWidth(TextView textView, String text) { if (textView != null) { LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); int width = DisplayUtil.px2dip(mContext, layoutParams.leftMargin) + DisplayUtil.px2dip(mContext, layoutParams.rightMargin) + getTextViewWidth(textView, text); if (width <= minColumnWidth) { return minColumnWidth; } else if (width > minColumnWidth && width <= maxColumnWidth) { return width; } else { return maxColumnWidth; } } return 0; } /** * 計算TextView高度 * @param textView * @param text * @return */ private int measureTextHeight(TextView textView,String text){ if (textView != null) { LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); int height = getTextViewHeight(textView, text); if (height<minRowHeight){ return minRowHeight; }else if(height>minRowHeight&&height<maxRowHeight){ return height; }else{ return maxRowHeight; } } return 0; } /** * 根據文字計算textview的高度 * * @param textView * @param text * @return */ private int getTextViewHeight(TextView textView, String text) { if (textView != null) { int width=measureTextWidth(textView,text); TextPaint textPaint = textView.getPaint(); StaticLayout staticLayout = new StaticLayout(text, textPaint, DisplayUtil.dip2px(mContext,width), Layout.Alignment.ALIGN_NORMAL, 1, 0, false); int height=DisplayUtil.px2dip(mContext,staticLayout.getHeight()); return height; } return 0; } /** * 根據文字計算textview的高度 * * @param view * @param text * @return */ private int getTextViewWidth(TextView view, String text) { if (view != null) { TextPaint paint = view.getPaint(); return DisplayUtil.px2dip(mContext, (int) paint.measureText(text)); } return 0; } /** * 構造滾動檢視 * * @param scrollView * @param datas * @param isFristRow 是否是第一行 */ private void createScollview(HorizontalScrollView scrollView, List<String> datas, boolean isFristRow) { //設定LinearLayout LinearLayout linearLayout = new LinearLayout(mContext); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); linearLayout.setLayoutParams(layoutParams);