TableView组件相关代码
@SuppressLint("ViewConstructor")
public class TableView extends LinearLayout {
@SuppressWarnings("deprecation")
private static LayoutParams FILL_FILL_LAYOUTPARAMS = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
private static LayoutParams WAP_WAP_LAYOUTPARAMS = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
private static Paint BLACK_PAINT = new Paint();
private static Paint WHITE_PAINT = new Paint();
private static Paint DARK_PAINT = new Paint();
static {
WHITE_PAINT.setColor(Color.WHITE);
BLACK_PAINT.setColor(Color.BLACK);
}
private int columnMaxTextNumber = 0;
private CAdapter cAdapter;
/** 标题空间 */
private String[] title;
private LinearLayout titleLayout;
private LinearLayout footerLayout;
private ListView listView;
/** 数据. */
private List<Map<String, Object>> data;
/** 列宽数据. */
private int[] itemWidth;
/** 当前选中行. */
private int selectedPosition = -1;
/** 自动列宽列. */
private int autoWidthIndex = -1;
private AdapterView.OnItemClickListener onItemClickListener;
/** 行背景颜色. */
private int[] rowsBackgroundColor;
/** 选中行背景颜色. */
private int selectedBackgroundColor = Color.argb(200, 224, 243, 250);
/** 标题背景颜色. */
private int titleBackgroundColor;
/** 标题字体颜色. */
private int titleTextColor = Color.argb(255, 100, 100, 100);
/** 内容字体颜色. */
private int contentTextColor = Color.argb(255, 100, 100, 100);
/** 标题字体大小. */
private float titleTextSize = 0;
/** 内容字体大小. */
private float contentTextSize = 0;
/**
* 初始化带标题ListView
*
* @param context
* 父级上下文
* @param title
* 标题数组
* @param data
* 内容列表
*/
// public TableView(Context context, String[] title, List<String[]> data) {
public TableView(Context context, List<Map<String, Object>> data, String[] listKeys) {
super(context);
this.title = listKeys;
this.data = data;
this.setColumnMaxTextNumber(10);
DARK_PAINT.setColor(getResources().getColor(R.color.dark1));
setOrientation(VERTICAL);// 设定纵向布局
setBackgroundColor(Color.TRANSPARENT);// 设定背景为白色
this.itemWidth = new int[title.length];// 预先设定好每列的宽
autoWidthIndex = this.itemWidth.length - 1;
calcColumnWidth();// 计算列宽
// 添加title位置
titleLayout = new LinearLayout(getContext());
titleLayout.setBackgroundResource(R.drawable.round_table_row_title_shape);
addView(titleLayout);
// 绘制标题面板
drawTitleLayout();
drawFooterLayoutAttachListview(true);
// 添加listview
listView = new ListView(getContext());
listView.setPadding(0, 2, 0, 0);
listView.addFooterView(footerLayout);
cAdapter = new CAdapter();
listView.setAdapter(cAdapter);
listView.setCacheColorHint(0);
listView.setLayoutParams(FILL_FILL_LAYOUTPARAMS);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(parent, view, position, id);
setSelectedPosition(position);
selectedPosition = position;
cAdapter.notifyDataSetChanged();
}
});
addView(listView);
}
/**
* 为ListView添加额外信息
*/
private void drawFooterLayoutAttachListview(boolean isShowPageInfo) {
// 添加footer
footerLayout = new LinearLayout(getContext());
footerLayout.setGravity(Gravity.CENTER);
footerLayout.setBackgroundResource(R.drawable.round_table_row_footer_shape);
if (!isShowPageInfo) {
TextView tv = new TextView(getContext());
tv.setText("数据加载完成...");
tv.setTextColor(titleTextColor);
tv.setLayoutParams(FILL_FILL_LAYOUTPARAMS);
tv.setGravity(Gravity.CENTER);
int dipVal = ViewUtils.dip2px(getContext(), 2);
tv.setPadding(dipVal, dipVal, dipVal, dipVal);
footerLayout.addView(tv);
} else {
// 分页信息
TextView tv = new TextView(getContext());
tv.setText("1/3");
tv.setTextColor(titleTextColor);
tv.setLayoutParams(WAP_WAP_LAYOUTPARAMS);
tv.setGravity(Gravity.CENTER);
int dipVal = ViewUtils.dip2px(getContext(), 2);
tv.setPadding(dipVal, dipVal, dipVal, dipVal);
// footerLayout.addView(tv);
//
Button prevBtn = new Button(getContext());
prevBtn.setBackgroundResource(R.drawable.round_border_shape);
prevBtn.setTextColor(titleTextColor);
prevBtn.setPadding(dipVal, dipVal, dipVal, dipVal);
prevBtn.setText("上一页");
//
Button nextBtn = new Button(getContext());
nextBtn.setBackgroundResource(R.drawable.round_border_shape);
nextBtn.setTextColor(titleTextColor);
nextBtn.setPadding(dipVal, dipVal, dipVal, dipVal);
nextBtn.setText("下一页");
//
footerLayout.addView(prevBtn);
footerLayout.addView(tv);
footerLayout.addView(nextBtn);
}
}
/**
* 整体有改变时,刷新显示
*/
public void definedSetChanged() {
calcColumnWidth();
drawTitleLayout();
cAdapter.notifyDataSetChanged();
}
/**
* 设置选中时的监听器
*
* @param onItemClickListener
*/
public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
/**
* 设置行背景颜色, 多个颜色可以作为间隔色
*
* @param color
* 行背景颜色,可以为多个
*/
public void setItemBackgroundColor(int... color) {
rowsBackgroundColor = color;
}
/**
* 数据总数
*/
public int getCount() {
if (data == null)
return 0;
return data.size();
}
/**
* 当前选中数据
*
* @param position
* @return
*/
public Map<String, Object> getItem(int position) {
if (data == null)
return null;
return data.get(position);
}
/**
* 设置当前选中位置
*
* @return
*/
public void setSelectedPosition(int selectedPosition) {
this.selectedPosition = selectedPosition;
}
/**
* 当前选中位置
*
* @return
*/
public int getSelectedPosition() {
return selectedPosition;
}
/**
* 设置被选中时的背景色
*
* @param color
*/
public void setSelectedBackgroundColor(int color) {
selectedBackgroundColor = color;
}
/**
* 设置标题背景色.
*
* @param color
*/
public void setTitleBackgroundColor(int color) {
titleBackgroundColor = color;
titleLayout.setBackgroundColor(titleBackgroundColor);
}
/**
* 设置标题文字颜色
*
* @param color
*/
public void setTitleTextColor(int color) {
titleTextColor = color;
for (int i = 0; i < titleLayout.getChildCount(); i++) {
((TextView) titleLayout.getChildAt(i)).setTextColor(titleTextColor);
}
}
/**
* 设置内容文字颜色
*
* @param color
*/
public void setContentTextColor(int color) {
contentTextColor = color;
}
/**
* 设置标题字体大小
*
* @param szie
*/
public void setTitleTextSize(float szie) {
titleTextSize = szie;
}
/**
* 设置内容字体大小
*
* @param szie
*/
public void setContentTextSize(float szie) {
contentTextSize = szie;
}
/**
*
* 设定哪列自动列宽 从0开始计算
*
* @param js
*/
public void setAutoColumnWidth(int[] js) {
for (int i = 0; i < titleLayout.getChildCount(); i++) {
TextView tv = ((TextView) titleLayout.getChildAt(i));
for (int j = 0; j < js.length; j++) {
if (i == js[j])
tv.setLayoutParams(FILL_FILL_LAYOUTPARAMS);
else {
tv.setLayoutParams(WAP_WAP_LAYOUTPARAMS);
tv.setWidth(itemWidth[i]);
}
}
}
}
/**
* 绘制标题
*/
private void drawTitleLayout() {
titleLayout.removeAllViews();
for (int i = 0; i < title.length; i++) {
boolean drawRight = (i == title.length - 1) ? false : true;
TextView tv = new CTextView(titleLayout.getContext(), drawRight, WHITE_PAINT);
tv.setTextColor(titleTextColor);
tv.setGravity(Gravity.CENTER);
tv.setText(title[i]);
if (titleTextSize > 0) {
tv.setTextSize(titleTextSize);
}
int dipVal = ViewUtils.dip2px(getContext(), 2);
tv.setPadding(dipVal, dipVal, dipVal, dipVal);
if (i == autoWidthIndex)
tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);
else {
tv.setWidth(itemWidth[i]);
}
titleLayout.addView(tv);
titleLayout.setBackgroundResource(R.drawable.round_table_row_title_shape);
}
}
/**
* 计算列宽
*
* @return 是否有改动
*/
private boolean calcColumnWidth() {
boolean result = false;
float textSize = new TextView(getContext()).getTextSize();
// 计算标题列宽
for (int i = 0; i < itemWidth.length; i++) {
int w = (int) TableView.GetPixelByText((titleTextSize > 0) ? titleTextSize : textSize, title[i]);
if (itemWidth[i] < w) {
itemWidth[i] = w;
result = true;
}
}
// 计算内容列宽
if (contentTextSize > 0) {
textSize = contentTextSize;
}
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < title.length; j++) {
//
String val = String.valueOf(data.get(i).get(title[j]));
if (getColumnMaxTextNumber() > 0 && val != null && val.length() >= getColumnMaxTextNumber()) {
val = val.substring(0, getColumnMaxTextNumber());
}
//
int w = (int) TableView.GetPixelByText(textSize, val);
if (itemWidth[j] < w) {
itemWidth[j] = w;
result = true;
}
}
}
return result;
}
/**
* 计算字符串所占像素
*
* @param textSize
* 字体大小
* @param text
* 字符串
* @return 字符串所占像素
*/
private static int GetPixelByText(float textSize, String text) {
Paint mTextPaint = new Paint();
mTextPaint.setTextSize(textSize); // 指定字体大小
mTextPaint.setFakeBoldText(true); // 粗体
mTextPaint.setAntiAlias(true); // 非锯齿效果
return (int) (mTextPaint.measureText(text) + textSize);
}
/**
* 获取每列显示的字符个数
*
* @return
*/
public int getColumnMaxTextNumber() {
return columnMaxTextNumber;
}
/**
* 设置每一列显示的字符个数
*
* @param columnMaxTextNumber
*/
public void setColumnMaxTextNumber(int columnMaxTextNumber) {
this.columnMaxTextNumber = columnMaxTextNumber;
}
/**
* 主要用的Adapter
*
* @author Cdisk
*
*/
class CAdapter extends BaseAdapter {
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
if (data == null)
return 0;
return data.size();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
if (data == null)
return null;
return data.get(position);
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return 0;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 初始化主layout
LinearLayout contextLayout = new LinearLayout(TableView.this.getContext());
Map<String, Object> dataItem = data.get(position);
if (getSelectedPosition() == position) { // 为当前选中行
contextLayout.setBackgroundColor(selectedBackgroundColor);
} else if (rowsBackgroundColor != null && rowsBackgroundColor.length > 0) {
contextLayout.setBackgroundColor(rowsBackgroundColor[position % rowsBackgroundColor.length]);
}
for (int i = 0; i < title.length; i++) {
boolean drawRight = (i == title.length - 1) ? false : true;
TextView tv = new CTextView(contextLayout.getContext(), drawRight, DARK_PAINT);
tv.setTextColor(contentTextColor);
tv.setGravity(Gravity.CENTER);
String val = String.valueOf(dataItem.get(title[i]));
if (i < dataItem.size()) {
if (getColumnMaxTextNumber() > 0 && val != null && val.length() >= getColumnMaxTextNumber()) {
val = val.substring(0, getColumnMaxTextNumber());
}
tv.setText(val);
}
if (i == autoWidthIndex)
tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);
else {
tv.setWidth(itemWidth[i]);
}
if (contentTextSize > 0) {
tv.setTextSize(contentTextSize);
}
contextLayout.addView(tv);
contextLayout.setBackgroundResource(R.drawable.round_table_row_content_shape);
}
return contextLayout;
}
}
/**
* 重写的TextView
*
* @author Cdisk
*/
class CTextView extends TextView {
private boolean isDrawSpliteLine = true;
private Paint paint;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawLine(0, 0, this.getWidth() - 1, 0, WHITE_PAINT);// Top
// canvas.drawLine(0, 0, 0, this.getHeight() - 1,
// WHITE_PAINT);//Left
if (isDrawSpliteLine) {
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);// Right
}
// canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
// this.getHeight() - 1, BLACK_PAINT);// Buttom
}
public CTextView(Context context) {
super(context);
}
public CTextView(Context context, boolean isDrawSpliteLine, Paint paint) {
super(context);
this.isDrawSpliteLine = isDrawSpliteLine;
this.paint = paint;
}
}
}
调用如下:
public class TableListActivity extends Activity {
TableView listView;
List<String[]> list = new ArrayList<String[]>();
private List<Map<String, Object>> dataSource = new ArrayList<Map<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataSource();
String[] title = new String[] { "id", "key1", "key2", "key3" };
listView = new TableView(this, dataSource, title);
listView.setBackgroundResource(R.drawable.round_shape);
listView.setTitleTextColor(Color.GREEN);
listView.setTitleBackgroundColor(Color.RED);
listView.setAutoColumnWidth(new int[] { 3, 4, 5 });
listView.setItemBackgroundColor(Color.GREEN, Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String, Object> item = listView.getItem(position);
item.put("key2", "12312312312");
listView.definedSetChanged();
}
});
listView.definedSetChanged();
//
LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(TableListActivity.this).inflate(R.layout.table_view_layout, null);
linearLayout.addView(listView);
setContentView(linearLayout);
}
private void initDataSource() {
for (int i = 0; i < 20; i++) {
Map<String, Object> varMap = new HashMap<String, Object>();
varMap.put("id", "编号" + i);
varMap.put("key1", "key1 * key1 * key1 * key1 * key1");
varMap.put("key2", "key2 * key2 * key2 * key2 * key2");
varMap.put("key3", "key3 * key3 * key3 * key3 * key3");
varMap.put("key4", "key4 * key4 * key4 * key4 * key4");
varMap.put("key5", "key5 * key5 * key5 * key5 * key5");
dataSource.add(varMap);
}
}
}