Open JiangShuLiang opened 5 years ago
你创建表格后,首行的宽度固定下来了,之后加载数据是局部刷新的,就会导致错位。我这边解决办法是用 setColumnWidth(int mColumnNum, int mColumnWidth)
固定下每一列的宽度,或者你可以每次获取到数据后重新刷新或创建表格
你创建表格后,首行的宽度固定下来了,之后加载数据是局部刷新的,就会导致错位。我这边解决办法是用
setColumnWidth(int mColumnNum, int mColumnWidth)
固定下每一列的宽度,或者你可以每次获取到数据后重新刷新或创建表格
你说的这个原因我之前也发现了。你说的两种解决方案我也都有考虑过,但:1.每列都固定有点麻烦,最终展现出的效果也可能会不太美观;2.如果重新创建表格的话,就又要滚回到第一行从头看了,不能从第一页的最后一条开始往下看,会有点破坏上拉加载更多的使用体验唉。我是有发emial给作者,但一直没收到回复,他应该是不打算再维护这个项目了,T_T
你创建表格后,首行的宽度固定下来了,之后加载数据是局部刷新的,就会导致错位。我这边解决办法是用
setColumnWidth(int mColumnNum, int mColumnWidth)
固定下每一列的宽度,或者你可以每次获取到数据后重新刷新或创建表格你说的这个原因我之前也发现了。你说的两种解决方案我也都有考虑过,但:1.每列都固定有点麻烦,最终展现出的效果也可能会不太美观;2.如果重新创建表格的话,就又要滚回到第一行从头看了,不能从第一页的最后一条开始往下看,会有点破坏上拉加载更多的使用体验唉。我是有发emial给作者,但一直没收到回复,他应该是不打算再维护这个项目了,T_T
或者你可以试试找找有没其他库,我是用固定每一列的宽度来实现的,虽然现在也比较少用这个功能了
我解决了 LockTableView.java
` package com.rmondjone.locktableview; import android.content.Context; import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; 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.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.rmondjone.xrecyclerview.ProgressStyle; import com.rmondjone.xrecyclerview.XRecyclerView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /**
表格⽗父视图
*/ private ViewGroup mContentView; /**
第⼀一⾏行行背景颜⾊色 */ private int mFristRowBackGroudColor; /**
Item⻓长按事件 */ private OnItemLongClickListenter mOnItemLongClickListenter;
/**
表格每⼀一⾏行行数据,不不包括第⼀一⾏行行和第⼀一列列
*/
private ArrayList<ArrayList
第⼀一⾏行行滚动视图(未锁状态) */ private CustomHorizontalScrollView mUnLockScrollView;
/**
初始化属性 */ private void initAttrs() { mTableView = LayoutInflater.from(mContext).inflate(R.layout.locktab leview, 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; mCellPadding=DisplayUtil.dip2px(mContext,45); } /**
初始化表格数据 */
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
}
// Log.e("每⾏行行最多个数",maxLength+"");
for (int i = 0; i < mTableDatas.size(); i++) { ArrayList
textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutPar ams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);// android:layout_margin="15dp" textView.setLayoutParams(textViewParams); if (i == 0) { mColumnMaxWidths.add(measureTextWidth(textVie w, 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.d("第"+i+"⾏行行列列最⼤大宽 度",buffer.toString()); } //如果⽤用户指定某列列宽度则按照⽤用户指定宽度 算 mChangeColumns.keySet()) { changeColumnWidth(key, if (mChangeColumns.size() > 0) { for (Integer key :
mChangeColumns.get(key)); Log.d("changeColumnWidth",
mChangeColumns.get(key) + ""); }
}
Log.d("每列列最⼤大宽度 dp:",mColumnMaxWidths.toString());
for (int i = 0;i < mHeadViews.size();i++) {
ViewGroup.LayoutParams params = mHeadViews.get(i).getLayoutParams();
if (isLockFristColumn) { params.width =
DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i + 1));
}else { params.width =
DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i));
}
mHeadViews.get(i).setLayoutParams(params); }
//初始化每⾏行行最⼤大⾼高度
for (int i = 0; i < mTableDatas.size(); i++) {
ArrayList
textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutPar ams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(mCellPadding,
mCellPadding, mCellPadding, mCellPadding);// 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;
//如果⽤用户指定某列列宽度则按照⽤用户指定 宽度算对应列列的⾼高度
if (mChangeColumns.size() > 0 && mChangeColumns.containsKey(j)) {
currentHeight = getTextViewHeight(textView, rowDatas.get(j),
mChangeColumns.get(j)); } else {
currentHeight = measureTextHeight(textView, rowDatas.get(j));
}
buffer.append("[" + currentHeight + "]"); if (currentHeight > maxHeight) {
mRowMaxHeights.set(i,
// dp:",mRowMaxHeights.toString());
if (isLockFristRow) { ArrayList
(ArrayList
mTableRowDatas.add(mTableDatas.get(i)); } } } } 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_Scroll View); mUnLockScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.unlockHeadView_Scr ollView); //表格主视图 mTableScrollView = (XRecyclerView) mTableView.findViewById(R.id.table_scrollView); LinearLayoutManager layoutManager = new Log.e("第⼀一⾏行行数据", // mTableFristData.toString()); // Log.e("第⼀一列列数据", mTableColumnDatas.toString()); // Log.e("每⾏行行数据", mTableRowDatas.toString());
LinearLayoutManager(mContext); layoutManager.setOrientation(LinearLayoutManager. VERTICAL); mTableScrollView.setLayoutManager(layoutManager) ; mTableScrollView.setArrowImageView(R.drawable.ic onfont_downgrey); mTableScrollView.setRefreshProgressStyle(Progress Style.BallRotate); mTableScrollView.setLoadingMoreProgressStyle(Pro gressStyle.BallRotate); mTableScrollView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { if (mOnLoadingListener != null) { mOnLoadingListener.onRefresh(mTableScrollView, mTableDatas); } } @Override public void onLoadMore() { if (mOnLoadingListener != null) { mOnLoadingListener.onLoadMore(mTableScrollView, mTableDatas); } } }); mTableViewAdapter = new TableViewAdapter(mContext, mTableColumnDatas, mTableRowDatas, isLockFristColumn, isLockFristRow);
mTableViewAdapter.setCellPadding(mCellPadding); mTableViewAdapter.setColumnMaxWidths(mColumn MaxWidths); mTableViewAdapter.setRowMaxHeights(mRowMaxH eights); mTableViewAdapter.setTextViewSize(mTextViewSize ); mTableViewAdapter.setTableContentTextColor(mTabl eContentTextColor); mTableViewAdapter.setTableHeadTextColor(mTableH eadTextColor); mTableViewAdapter.setFristRowBackGroudColor(mF ristRowBackGroudColor); mTableViewAdapter.setHorizontalScrollView(new OnTableViewListener() { @Override public void onTableViewScrollChange(int x, int y) { changeAllScrollView(x, y); } }); if (mOnItemClickListenter != null) { mTableViewAdapter.setOnItemClickListenter(mOnIte mClickListenter); } if (mOnItemLongClickListenter != null) { mTableViewAdapter.setOnItemLongClickListenter(m OnItemLongClickListenter); } if (mOnItemSeletor != 0) { mTableViewAdapter.setOnItemSeletor(mOnItemSelet
or); } else { mTableViewAdapter.setOnItemSeletor(R.color.dashli ne_color); } mTableViewAdapter.setTableViewRangeListener(new OnTableViewRangeListener() { @Override public void onLeft(HorizontalScrollView view) if (mTableViewRangeListener != null) { mTableViewRangeListener.onLeft(view); } } @Override public void onRight(HorizontalScrollView view) { if (mTableViewRangeListener != null) { mTableViewRangeListener.onRight(view); } } }); mTableViewAdapter.setOnTableViewCreatedListener( new TableViewAdapter.OnTableViewCreatedListener() { @Override public void onTableViewCreatedCompleted(CustomHorizontalSc rollView mScrollView) { mScrollViews.add(mScrollView); } }); mTableScrollView.setAdapter(mTableViewAdapter); {
mLockHeadView.setBackgroundColor(ContextComp at.getColor(mContext, mFristRowBackGroudColor)); mUnLockHeadView.setBackgroundColor(ContextCo mpat.getColor(mContext, mFristRowBackGroudColor)); if (isLockFristRow) { if (isLockFristColumn) { mLockHeadView.setVisibility(View.VISIBLE); mUnLockHeadView.setVisibility(View.GONE); } else { mLockHeadView.setVisibility(View.GONE); mUnLockHeadView.setVisibility(View.VISIBLE); } createHeadView(); } else { mLockHeadView.setVisibility(View.GONE); mUnLockHeadView.setVisibility(View.GONE); } } /**
创建头部视图 */ private void createHeadView() { if (isLockFristColumn) { mColumnTitleView.setTextColor(ContextCompat.getC olor(mContext, mTableHeadTextColor)); mColumnTitleView.setTextSize(TypedValue.COMPLE X_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(mCellPadding, mCellPadding, mCellPadding, mCellPadding); 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); } @Override public void onScrollFarLeft(HorizontalScrollView scrollView) { if (mTableViewRangeListener != null) { mTableViewRangeListener.onLeft(scrollView); } } @Override public void onScrollFarRight(HorizontalScrollView scrollView) { if (mTableViewRangeListener != null) { mTableViewRangeListener.onRight(scrollView); } } });
} 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); } @Override public void onScrollFarLeft(HorizontalScrollView scrollView) { if (mTableViewRangeListener != null) { mTableViewRangeListener.onLeft(scrollView); } } @Override public void onScrollFarRight(HorizontalScrollView scrollView) { if (mTableViewRangeListener != null) { mTableViewRangeListener.onRight(scrollView); } } }); } } /**
@param x
@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; } /**
@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 isFristRow 是否是第⼀一⾏行行 */
private void createScollview(HorizontalScrollView scrollView, List
linearLayout.setOrientation(LinearLayout.HORIZONT AL); for (int i = 0; i < datas.size(); i++) { //构造单元格 TextView textView = new TextView(mContext); if (isFristRow) { mHeadViews.add(textView); textView.setTextColor(ContextCompat.getColor(mCo ntext, mTableHeadTextColor)); } else { textView.setTextColor(ContextCompat.getColor(mCo ntext, mTableContentTextColor)); } textView.setTextSize(TypedValue.COMPLEXUNIT SP, mTextViewSize); textView.setGravity(Gravity.CENTER); textView.setText(datas.get(i)); //设置布局 LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutPar ams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding); textView.setLayoutParams(textViewParams); ViewGroup.LayoutParams textViewParamsCopy = textView.getLayoutParams(); if (isLockFristColumn) { textViewParamsCopy.width = DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i + 1)); } else { textViewParamsCopy.width =
DisplayUtil.dip2px(mContext, mColumnMaxWidths.get(i)); } linearLayout.addView(textView); //画分隔线 if (i != datas.size() - 1) { View splitView = new View(mContext); ViewGroup.LayoutParams splitViewParmas = new ViewGroup.LayoutParams(DisplayUtil.dip2px(mCont ext, 1), ViewGroup.LayoutParams.MATCH_PARENT); splitView.setLayoutParams(splitViewParmas); if (isFristRow) { splitView.setBackgroundColor(ContextCompat.getCol or(mContext, R.color.white)); } else { splitView.setBackgroundColor(ContextCompat.getCol or(mContext, R.color.light_gray)); } linearLayout.addView(splitView); } } scrollView.addView(linearLayout); } /**
@param mColumnWidth */ private void changeColumnWidth(int mColumnNum, int mColumnWidth) {
if (mColumnMaxWidths != null && mColumnMaxWidths.size() > 0) { if (mColumnNum < mColumnMaxWidths.size() && mColumnNum >= 0) { mColumnMaxWidths.set(mColumnNum, mColumnWidth + DisplayUtil.px2dip(mContext, 15) * 2); } else { Log.e("LockTableView", "指定列列数不不存在"); } } } //属性设置 public LockTableView setLockFristRow(boolean lockFristRow) { isLockFristRow = lockFristRow; return this; } public LockTableView setLockFristColumn(boolean lockFristColumn) { isLockFristColumn = lockFristColumn; return this; } public LockTableView setMaxColumnWidth(int maxColumnWidth) { this.maxColumnWidth = maxColumnWidth; return this; } public LockTableView setMinColumnWidth(int minColumnWidth) { this.minColumnWidth = minColumnWidth; return this; } public LockTableView setFristRowBackGroudColor(int
mFristRowBackGroudColor) { this.mFristRowBackGroudColor = mFristRowBackGroudColor; return this; } public LockTableView setNullableString(String mNullableString) { this.mNullableString = mNullableString; return this; } public LockTableView setTextViewSize(int mTextViewSize) { this.mTextViewSize = mTextViewSize; return this; } public LockTableView setTableHeadTextColor(int mTableHeadTextColor) { this.mTableHeadTextColor = mTableHeadTextColor; return this; } public LockTableView setTableContentTextColor(int mTableContentTextColor) { this.mTableContentTextColor = mTableContentTextColor; return this; } public LockTableView setMaxRowHeight(int maxRowHeight) { this.maxRowHeight = maxRowHeight; return this; } public LockTableView setMinRowHeight(int minRowHeight) {
this.minRowHeight = minRowHeight; return this; } public LockTableView setTableViewListener(OnTableViewListener mTableViewListener) { this.mTableViewListener = mTableViewListener; return this; } public LockTableView setOnLoadingListener(OnLoadingListener mOnLoadingListener) { this.mOnLoadingListener = mOnLoadingListener; return this; } public LockTableView setTableViewRangeListener(OnTableViewRangeListe ner mTableViewRangeListener) { this.mTableViewRangeListener = mTableViewRangeListener; return this; } public LockTableView setOnItemClickListenter(OnItemClickListenter mOnItemClickListenter) { this.mOnItemClickListenter = mOnItemClickListenter; return this; } public LockTableView setOnItemLongClickListenter(OnItemLongClickListen ter mOnItemLongClickListenter) { this.mOnItemLongClickListenter = mOnItemLongClickListenter; return this;
} public LockTableView setOnItemSeletor(int mOnItemSeletor) { this.mOnItemSeletor = mOnItemSeletor; return this; } public LockTableView setCellPadding(int mCellPadding) { this.mCellPadding = DisplayUtil.dip2px(mContext,mCellPadding); return this; } /**
@return */
public LockTableView setColumnWidth(int mColumnNum, int mColumnWidth) {
//判断是否已经设置过
if (mChangeColumns.containsKey(mColumnNum)) {
mChangeColumns.remove(mColumnNum); }
mChangeColumns.put(mColumnNum, mColumnWidth);
return this; }
//值获取
public ArrayList
}
public LinearLayout getLockHeadView() { return mLockHeadView;
}
public LinearLayout getUnLockHeadView() { return mUnLockHeadView;
}
public XRecyclerView getTableScrollView() { return mTableScrollView;
}
public ArrayList
@param mTableDatas */
public void setTableDatas(ArrayList<ArrayList
上拉刷新,下拉加载
*/ public interface OnLoadingListener { /**
@param position 点击位置 */
void onItemClick(View item, int position); } /**
解决思路就是记录head所有的TextView,然后在initData里动态调整宽度就好了
表格数据是通过分页请求接口填充的,现象如图