Android ListView实现Table行列效果

Android ListView实现Table行列效果

先来看运行效果:


就两个类,放上去运行就可以看到效果。

TableView.java
Java代码 复制代码  收藏代码
  1. package com.iaiai;   
  2.   
  3. import java.util.List;   
  4.   
  5. import android.content.Context;   
  6. import android.graphics.Canvas;   
  7. import android.graphics.Color;   
  8. import android.graphics.Paint;   
  9. import android.view.Gravity;   
  10. import android.view.View;   
  11. import android.view.ViewGroup;   
  12. import android.widget.AdapterView;   
  13. import android.widget.BaseAdapter;   
  14. import android.widget.LinearLayout;   
  15. import android.widget.ListView;   
  16. import android.widget.TextView;   
  17.   
  18. /**  
  19.  *   
  20.  * <p>  
  21.  * Title: MainActivity.java  
  22.  * </p>  
  23.  * <p>  
  24.  * E-Mail: 176291935@qq.com  
  25.  * </p>  
  26.  * <p>  
  27.  * QQ: 176291935  
  28.  * </p>  
  29.  * <p>  
  30.  * Http: iaiai.iteye.com  
  31.  * </p>  
  32.  * <p>  
  33.  * Create time: 2011-11-9  
  34.  * </p>  
  35.  *   
  36.  * @author 丸子  
  37.  * @version 0.0.1  
  38.  */  
  39. public class TableView extends LinearLayout {   
  40.   
  41.     private static LayoutParams FILL_FILL_LAYOUTPARAMS = new LayoutParams(   
  42.             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);   
  43.     private static LayoutParams WAP_WAP_LAYOUTPARAMS = new LayoutParams(   
  44.             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   
  45.   
  46.     private static Paint BLACK_PAINT = new Paint();   
  47.     private static Paint WHITE_PAINT = new Paint();   
  48.     static {   
  49.         WHITE_PAINT.setColor(Color.WHITE);   
  50.         BLACK_PAINT.setColor(Color.BLACK);   
  51.     }   
  52.   
  53.     private CAdapter cAdapter;   
  54.   
  55.     /** 标题空间. */  
  56.     private LinearLayout titleLayout;   
  57.     private String[] title;   
  58.   
  59.     private ListView listView;   
  60.     /** 数据. */  
  61.     private List<String[]> data;   
  62.   
  63.     /** 列宽数据. */  
  64.     private int[] itemWidth;   
  65.   
  66.     /** 当前选中行. */  
  67.     private int selectedPosition = -1;   
  68.     /** 自动列宽列. */  
  69.     private int autoWidthIndex = -1;   
  70.   
  71.     private AdapterView.OnItemClickListener onItemClickListener;   
  72.   
  73.     /** 行背景颜色. */  
  74.     private int[] rowsBackgroundColor;   
  75.     /** 选中行背景颜色. */  
  76.     private int selectedBackgroundColor = Color.argb(200224243250);   
  77.     /** 标题背景颜色. */  
  78.     private int titleBackgroundColor;   
  79.     /** 标题字体颜色. */  
  80.     private int titleTextColor = Color.argb(255100100100);   
  81.     /** 内容字体颜色. */  
  82.     private int contentTextColor = Color.argb(255100100100);   
  83.     /** 标题字体大小. */  
  84.     private float titleTextSize = 0;   
  85.     /** 内容字体大小. */  
  86.     private float contentTextSize = 0;   
  87.   
  88.     /**  
  89.      * 初始化带标题ListView  
  90.      *   
  91.      * @param context  
  92.      *            父级上下文  
  93.      * @param title  
  94.      *            标题数组  
  95.      * @param data  
  96.      *            内容列表  
  97.      */  
  98.     public TableView(Context context, String[] title, List<String[]> data) {   
  99.         super(context);   
  100.   
  101.         this.title = title;   
  102.         this.data = data;   
  103.   
  104.         // 设定纵向布局   
  105.         setOrientation(VERTICAL);   
  106.         // 设定背景为白色   
  107.         setBackgroundColor(Color.WHITE);   
  108.   
  109.         // 预先设定好每列的宽   
  110.         this.itemWidth = new int[title.length];   
  111.         autoWidthIndex = this.itemWidth.length - 1;   
  112.         // 计算列宽   
  113.         calcColumnWidth();   
  114.   
  115.         // 添加title位置   
  116.         titleLayout = new LinearLayout(getContext());   
  117.         titleLayout.setBackgroundColor(Color.parseColor("#CCCCCC"));   
  118.         addView(titleLayout);   
  119.         // 绘制标题面板   
  120.         drawTitleLayout();   
  121.   
  122.         // 添加listview   
  123.         listView = new ListView(getContext());   
  124.         listView.setPadding(0200);   
  125.         cAdapter = new CAdapter();   
  126.         listView.setAdapter(cAdapter);   
  127.         listView.setCacheColorHint(0);   
  128.         listView.setLayoutParams(FILL_FILL_LAYOUTPARAMS);   
  129.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {   
  130.   
  131.             @Override  
  132.             public void onItemClick(AdapterView<?> parent, View view,   
  133.                     int position, long id) {   
  134.                 if (onItemClickListener != null)   
  135.                     onItemClickListener.onItemClick(parent, view, position, id);   
  136.                 setSelectedPosition(position);   
  137.                 selectedPosition = position;   
  138.                 cAdapter.notifyDataSetChanged();   
  139.             }   
  140.         });   
  141.         addView(listView);   
  142.     }   
  143.   
  144.     /**  
  145.      * 整体有改变时,刷新显示  
  146.      */  
  147.     public void definedSetChanged() {   
  148.         calcColumnWidth();   
  149.         drawTitleLayout();   
  150.         cAdapter.notifyDataSetChanged();   
  151.     }   
  152.   
  153.     /**  
  154.      * 设置选中时的监听器  
  155.      *   
  156.      * @param onItemClickListener  
  157.      */  
  158.     public void setOnItemClickListener(   
  159.             AdapterView.OnItemClickListener onItemClickListener) {   
  160.         this.onItemClickListener = onItemClickListener;   
  161.     }   
  162.   
  163.     /**  
  164.      * 设置行背景颜色, 多个颜色可以作为间隔色  
  165.      *   
  166.      * @param color  
  167.      *            行背景颜色,可以为多个  
  168.      */  
  169.     public void setItemBackgroundColor(int... color) {   
  170.         rowsBackgroundColor = color;   
  171.     }   
  172.   
  173.     /**  
  174.      * 数据总数  
  175.      */  
  176.     public int getCount() {   
  177.         if (data == null)   
  178.             return 0;   
  179.         return data.size();   
  180.     }   
  181.   
  182.     /**  
  183.      * 当前选中数据  
  184.      *   
  185.      * @param position  
  186.      * @return  
  187.      */  
  188.     public String[] getItem(int position) {   
  189.         if (data == null)   
  190.             return null;   
  191.         return data.get(position);   
  192.     }   
  193.   
  194.     /**  
  195.      * 设置当前选中位置  
  196.      *   
  197.      * @return  
  198.      */  
  199.     public void setSelectedPosition(int selectedPosition) {   
  200.         this.selectedPosition = selectedPosition;   
  201.     }   
  202.   
  203.     /**  
  204.      * 当前选中位置  
  205.      *   
  206.      * @return  
  207.      */  
  208.     public int getSelectedPosition() {   
  209.         return selectedPosition;   
  210.     }   
  211.   
  212.     /**  
  213.      * 设置被选中时的背景色  
  214.      *   
  215.      * @param color  
  216.      */  
  217.     public void setSelectedBackgroundColor(int color) {   
  218.         selectedBackgroundColor = color;   
  219.     }   
  220.   
  221.     /**  
  222.      * 设置标题背景色.  
  223.      *   
  224.      * @param color  
  225.      */  
  226.     public void setTitleBackgroundColor(int color) {   
  227.         titleBackgroundColor = color;   
  228.         titleLayout.setBackgroundColor(titleBackgroundColor);   
  229.     }   
  230.   
  231.     /**  
  232.      * 设置标题文字颜色  
  233.      *   
  234.      * @param color  
  235.      */  
  236.     public void setTitleTextColor(int color) {   
  237.         titleTextColor = color;   
  238.         for (int i = 0; i < titleLayout.getChildCount(); i++) {   
  239.             ((TextView) titleLayout.getChildAt(i)).setTextColor(titleTextColor);   
  240.         }   
  241.     }   
  242.   
  243.     /**  
  244.      * 设置内容文字颜色  
  245.      *   
  246.      * @param color  
  247.      */  
  248.     public void setContentTextColor(int color) {   
  249.         contentTextColor = color;   
  250.     }   
  251.   
  252.     /**  
  253.      * 设置标题字体大小  
  254.      *   
  255.      * @param szie  
  256.      */  
  257.     public void setTitleTextSize(float szie) {   
  258.         titleTextSize = szie;   
  259.     }   
  260.   
  261.     /**  
  262.      * 设置内容字体大小  
  263.      *   
  264.      * @param szie  
  265.      */  
  266.     public void setContentTextSize(float szie) {   
  267.         contentTextSize = szie;   
  268.     }   
  269.   
  270.     /**  
  271.      *   
  272.      * 设定哪列自动列宽 从0开始计算  
  273.      *   
  274.      * @param index  
  275.      */  
  276.     public void setAutoColumnWidth(int index) {   
  277.         autoWidthIndex = index;   
  278.         for (int i = 0; i < titleLayout.getChildCount(); i++) {   
  279.             TextView tv = ((TextView) titleLayout.getChildAt(i));   
  280.             if (i == autoWidthIndex)   
  281.                 tv.setLayoutParams(FILL_FILL_LAYOUTPARAMS);   
  282.             else {   
  283.                 tv.setLayoutParams(WAP_WAP_LAYOUTPARAMS);   
  284.                 tv.setWidth(itemWidth[i]);   
  285.             }   
  286.         }   
  287.     }   
  288.   
  289.     /**  
  290.      * 绘制标题  
  291.      */  
  292.     private void drawTitleLayout() {   
  293.         titleLayout.removeAllViews();   
  294.         for (int i = 0; i < title.length; i++) {   
  295.             TextView tv = new CTextView(titleLayout.getContext());   
  296.             tv.setTextColor(titleTextColor);   
  297.             tv.setGravity(Gravity.CENTER);   
  298.             tv.setText(title[i]);   
  299.             if (titleTextSize > 0) {   
  300.                 tv.setTextSize(titleTextSize);   
  301.             }   
  302.             tv.setPadding(5050);   
  303.             if (i == autoWidthIndex)   
  304.                 tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);   
  305.             else {   
  306.                 tv.setWidth(itemWidth[i]);   
  307.             }   
  308.             titleLayout.addView(tv);   
  309.         }   
  310.     }   
  311.   
  312.     /**  
  313.      * 计算列宽  
  314.      *   
  315.      * @return 是否有改动  
  316.      */  
  317.     private boolean calcColumnWidth() {   
  318.         boolean result = false;   
  319.   
  320.         float textSize = new TextView(getContext()).getTextSize();   
  321.   
  322.         // 计算标题列宽   
  323.         for (int i = 0; i < itemWidth.length; i++) {   
  324.             int w = (int) TableView.GetPixelByText(   
  325.                     (titleTextSize > 0) ? titleTextSize : textSize, title[i]);   
  326.             if (itemWidth[i] < w) {   
  327.                 itemWidth[i] = w;   
  328.                 result = true;   
  329.             }   
  330.         }   
  331.   
  332.         // 计算内容列宽   
  333.         if (contentTextSize > 0) {   
  334.             textSize = contentTextSize;   
  335.         }   
  336.         for (int i = 0; i < data.size(); i++) {   
  337.             for (int j = 0; j < itemWidth.length && j < data.get(i).length; j++) {   
  338.                 int w = (int) TableView   
  339.                         .GetPixelByText(textSize, data.get(i)[j]);   
  340.                 if (itemWidth[j] < w) {   
  341.                     itemWidth[j] = w;   
  342.                     result = true;   
  343.                 }   
  344.             }   
  345.         }   
  346.         return result;   
  347.     }   
  348.   
  349.     /**  
  350.      * 计算字符串所占像素  
  351.      *   
  352.      * @param textSize  
  353.      *            字体大小  
  354.      * @param text  
  355.      *            字符串  
  356.      * @return 字符串所占像素  
  357.      */  
  358.     private static int GetPixelByText(float textSize, String text) {   
  359.         Paint mTextPaint = new Paint();   
  360.         mTextPaint.setTextSize(textSize); // 指定字体大小   
  361.         mTextPaint.setFakeBoldText(true); // 粗体   
  362.         mTextPaint.setAntiAlias(true); // 非锯齿效果   
  363.   
  364.         return (int) (mTextPaint.measureText(text) + textSize);   
  365.     }   
  366.   
  367.     /**  
  368.      * 主要用的Adapter  
  369.      *   
  370.      * @author Cdisk  
  371.      *   
  372.      */  
  373.     class CAdapter extends BaseAdapter {   
  374.   
  375.         /*  
  376.          * (non-Javadoc)  
  377.          *   
  378.          * @see android.widget.Adapter#getCount()  
  379.          */  
  380.         @Override  
  381.         public int getCount() {   
  382.             if (data == null)   
  383.                 return 0;   
  384.             return data.size();   
  385.         }   
  386.   
  387.         /*  
  388.          * (non-Javadoc)  
  389.          *   
  390.          * @see android.widget.Adapter#getItem(int)  
  391.          */  
  392.         @Override  
  393.         public Object getItem(int position) {   
  394.             if (data == null)   
  395.                 return null;   
  396.             return data.get(position);   
  397.         }   
  398.   
  399.         /*  
  400.          * (non-Javadoc)  
  401.          *   
  402.          * @see android.widget.Adapter#getItemId(int)  
  403.          */  
  404.         @Override  
  405.         public long getItemId(int position) {   
  406.             return 0;   
  407.         }   
  408.   
  409.         /*  
  410.          * (non-Javadoc)  
  411.          *   
  412.          * @see android.widget.Adapter#getView(int, android.view.View,  
  413.          * android.view.ViewGroup)  
  414.          */  
  415.         @Override  
  416.         public View getView(int position, View convertView, ViewGroup parent) {   
  417.   
  418.             // 初始化主layout   
  419.             LinearLayout contextLayout = new LinearLayout(   
  420.                     TableView.this.getContext());   
  421.   
  422.             String[] dataItem = data.get(position);   
  423.   
  424.             if (getSelectedPosition() == position) { // 为当前选中行   
  425.                 contextLayout.setBackgroundColor(selectedBackgroundColor);   
  426.             } else if (rowsBackgroundColor != null  
  427.                     && rowsBackgroundColor.length > 0) {   
  428.                 contextLayout.setBackgroundColor(rowsBackgroundColor[position   
  429.                         % rowsBackgroundColor.length]);   
  430.             }   
  431.   
  432.             for (int i = 0; i < title.length; i++) {   
  433.                 TextView tv = new CTextView(contextLayout.getContext());   
  434.                 tv.setTextColor(contentTextColor);   
  435.                 tv.setGravity(Gravity.CENTER);   
  436.                 if (i < dataItem.length) {   
  437.                     tv.setText(dataItem[i]);   
  438.                 }   
  439.                 if (i == autoWidthIndex)   
  440.                     tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);   
  441.                 else {   
  442.                     tv.setWidth(itemWidth[i]);   
  443.                 }   
  444.                 if (contentTextSize > 0) {   
  445.                     tv.setTextSize(contentTextSize);   
  446.                 }   
  447.                 contextLayout.addView(tv);   
  448.             }   
  449.   
  450.             return contextLayout;   
  451.         }   
  452.   
  453.     }   
  454.   
  455.     /**  
  456.      * 重写的TextView  
  457.      *   
  458.      * @author Cdisk  
  459.      */  
  460.     class CTextView extends TextView {   
  461.   
  462.         @Override  
  463.         protected void onDraw(Canvas canvas) {   
  464.             super.onDraw(canvas);   
  465.             // Top   
  466.             canvas.drawLine(00this.getWidth() - 10, WHITE_PAINT);   
  467.             // Left   
  468.             canvas.drawLine(000this.getHeight() - 1, WHITE_PAINT);   
  469.             // Right   
  470.             canvas.drawLine(this.getWidth() - 10this.getWidth() - 1,   
  471.                     this.getHeight() - 1, BLACK_PAINT);   
  472.             // Buttom   
  473.             canvas.drawLine(0this.getHeight() - 1this.getWidth() - 1,   
  474.                     this.getHeight() - 1, BLACK_PAINT);   
  475.         }   
  476.   
  477.         public CTextView(Context context) {   
  478.             super(context);   
  479.         }   
  480.     }   
  481.   
  482. }  
package com.iaiai;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * <p>
 * Title: MainActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-9
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class TableView extends LinearLayout {

	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();
	static {
		WHITE_PAINT.setColor(Color.WHITE);
		BLACK_PAINT.setColor(Color.BLACK);
	}

	private CAdapter cAdapter;

	/** 标题空间. */
	private LinearLayout titleLayout;
	private String[] title;

	private ListView listView;
	/** 数据. */
	private List<String[]> 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) {
		super(context);

		this.title = title;
		this.data = data;

		// 设定纵向布局
		setOrientation(VERTICAL);
		// 设定背景为白色
		setBackgroundColor(Color.WHITE);

		// 预先设定好每列的宽
		this.itemWidth = new int[title.length];
		autoWidthIndex = this.itemWidth.length - 1;
		// 计算列宽
		calcColumnWidth();

		// 添加title位置
		titleLayout = new LinearLayout(getContext());
		titleLayout.setBackgroundColor(Color.parseColor("#CCCCCC"));
		addView(titleLayout);
		// 绘制标题面板
		drawTitleLayout();

		// 添加listview
		listView = new ListView(getContext());
		listView.setPadding(0, 2, 0, 0);
		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);
	}

	/**
	 * 整体有改变时,刷新显示
	 */
	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 String[] 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 index
	 */
	public void setAutoColumnWidth(int index) {
		autoWidthIndex = index;
		for (int i = 0; i < titleLayout.getChildCount(); i++) {
			TextView tv = ((TextView) titleLayout.getChildAt(i));
			if (i == autoWidthIndex)
				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++) {
			TextView tv = new CTextView(titleLayout.getContext());
			tv.setTextColor(titleTextColor);
			tv.setGravity(Gravity.CENTER);
			tv.setText(title[i]);
			if (titleTextSize > 0) {
				tv.setTextSize(titleTextSize);
			}
			tv.setPadding(5, 0, 5, 0);
			if (i == autoWidthIndex)
				tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);
			else {
				tv.setWidth(itemWidth[i]);
			}
			titleLayout.addView(tv);
		}
	}

	/**
	 * 计算列宽
	 * 
	 * @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 < itemWidth.length && j < data.get(i).length; j++) {
				int w = (int) TableView
						.GetPixelByText(textSize, data.get(i)[j]);
				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);
	}

	/**
	 * 主要用的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());

			String[] 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++) {
				TextView tv = new CTextView(contextLayout.getContext());
				tv.setTextColor(contentTextColor);
				tv.setGravity(Gravity.CENTER);
				if (i < dataItem.length) {
					tv.setText(dataItem[i]);
				}
				if (i == autoWidthIndex)
					tv.setLayoutParams(TableView.FILL_FILL_LAYOUTPARAMS);
				else {
					tv.setWidth(itemWidth[i]);
				}
				if (contentTextSize > 0) {
					tv.setTextSize(contentTextSize);
				}
				contextLayout.addView(tv);
			}

			return contextLayout;
		}

	}

	/**
	 * 重写的TextView
	 * 
	 * @author Cdisk
	 */
	class CTextView extends TextView {

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			// Top
			canvas.drawLine(0, 0, this.getWidth() - 1, 0, WHITE_PAINT);
			// Left
			canvas.drawLine(0, 0, 0, this.getHeight() - 1, WHITE_PAINT);
			// Right
			canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
					this.getHeight() - 1, BLACK_PAINT);
			// Buttom
			canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
					this.getHeight() - 1, BLACK_PAINT);
		}

		public CTextView(Context context) {
			super(context);
		}
	}

}


MainActivity.java
Java代码 复制代码  收藏代码
  1. package com.iaiai;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import android.app.Activity;   
  7. import android.graphics.Color;   
  8. import android.os.Bundle;   
  9. import android.view.Menu;   
  10. import android.view.MenuItem;   
  11. import android.view.View;   
  12. import android.widget.AdapterView;   
  13.   
  14. /**  
  15.  *   
  16.  * <p>  
  17.  * Title: MainActivity.java  
  18.  * </p>  
  19.  * <p>  
  20.  * E-Mail: 176291935@qq.com  
  21.  * </p>  
  22.  * <p>  
  23.  * QQ: 176291935  
  24.  * </p>  
  25.  * <p>  
  26.  * Http: iaiai.iteye.com  
  27.  * </p>  
  28.  * <p>  
  29.  * Create time: 2011-11-9  
  30.  * </p>  
  31.  *   
  32.  * @author 丸子  
  33.  * @version 0.0.1  
  34.  */  
  35. public class MainActivity extends Activity {   
  36.   
  37.     TableView listView;   
  38.     List<String[]> list = new ArrayList<String[]>();   
  39.   
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {   
  42.         super.onCreate(savedInstanceState);   
  43.   
  44.         makeData();   
  45.   
  46.         String[] title = new String[] { "ID""标题2""标题3""标题4""标题5" };   
  47.   
  48.         listView = new TableView(this, title, list);   
  49.         listView.setTitleTextColor(Color.GREEN);   
  50.         listView.setTitleBackgroundColor(Color.RED);   
  51.         // listView.setAutoColumnWidth(3);   
  52.         // listView.setItemBackgroundColor(Color.GREEN, Color.WHITE);   
  53.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {   
  54.   
  55.             @Override  
  56.             public void onItemClick(AdapterView<?> parent, View view,   
  57.                     int position, long id) {   
  58.                 String[] item = listView.getItem(position);   
  59.                 item[2] = "12312312312";   
  60.                 listView.definedSetChanged();   
  61.             }   
  62.         });   
  63.   
  64.         // listView.definedSetChanged();   
  65.   
  66.         setContentView(listView);   
  67.     }   
  68.   
  69.     /*  
  70.      * (non-Javadoc)  
  71.      *   
  72.      * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)  
  73.      */  
  74.     @Override  
  75.     public boolean onCreateOptionsMenu(Menu menu) {   
  76.         menu.add(000"测试修改选中行");   
  77.         return true;   
  78.     }   
  79.   
  80.     /*  
  81.      * (non-Javadoc)  
  82.      *   
  83.      * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)  
  84.      */  
  85.     @Override  
  86.     public boolean onOptionsItemSelected(MenuItem item) {   
  87.         switch (item.getItemId()) {   
  88.         case 0:   
  89.             listView.setSelectedPosition(32);   
  90.             break;   
  91.         }   
  92.         return true;   
  93.     }   
  94.   
  95.     private void makeData() {   
  96.         list.add(new String[] { "1""数据1.2""数据1.3""数据1.4" });   
  97.         list.add(new String[] { "2""数据2.2""数据2.3""数据2.4""数据2.5" });   
  98.         list.add(new String[] { "3""数据3.2""数据3.3""数据3.4" });   
  99.         list.add(new String[] { "4""数据4.2""数据4.3""数据4.4" });   
  100.         list.add(new String[] { "5""数据5.2""数据5.3""数据5.4""数据5.5" });   
  101.         list.add(new String[] { "6""数据6.2""数据6.3""数据6.4" });   
  102.         list.add(new String[] { "7""数据7.2""数据7.3""数据7.4""数据7.5",   
  103.                 "数据7.6" });   
  104.         list.add(new String[] { "8""数据8.2""数据8.3""数据8.4" });   
  105.         list.add(new String[] { "9""数据9.2""数据9.3""数据9.4" });   
  106.         list.add(new String[] { "10""数据10.2""数据10.3""数据10.4""数据10.5",   
  107.                 "数据10.6""数据10.7" });   
  108.         list.add(new String[] { "11""数据11.2""数据11.3""数据11.4""数据11.5" });   
  109.         list.add(new String[] { "12""数据12.2""数据12.3""数据12.4""数据12.5" });   
  110.         list.add(new String[] { "13""数据13.2""数据13.3""数据13.4""数据13.5" });   
  111.         list.add(new String[] { "14""数据14.2""数据14.3""数据14.4""数据14.5" });   
  112.         list.add(new String[] { "15""数据15.2""数据15.3""数据15.4""数据15.5" });   
  113.         list.add(new String[] { "16""数据16.2""数据16.3""数据16.4""数据16.5" });   
  114.         list.add(new String[] { "17""数据17.2""数据17.3""数据17.4""数据17.5" });   
  115.         list.add(new String[] { "18""数据18.2""数据18.3""数据18.4""数据18.5" });   
  116.         list.add(new String[] { "19""数据19.2""数据19.3""数据19.4""数据19.5" });   
  117.         list.add(new String[] { "20""数据20.2""数据20.3""数据20.4""数据20.5" });   
  118.         list.add(new String[] { "21""数据21.2""数据21.3""数据21.4""数据21.5" });   
  119.         list.add(new String[] { "22""数据22.2""数据22.3""数据22.4""数据22.5" });   
  120.         list.add(new String[] { "23""数据23.2""数据23.3""数据23.4""数据23.5" });   
  121.         list.add(new String[] { "24""数据24.2""数据24.3""数据24.4""数据24.5" });   
  122.         list.add(new String[] { "25""数据25.2""数据25.3""数据25.4""数据25.5" });   
  123.         list.add(new String[] { "26""数据26.2" });   
  124.         list.add(new String[] { "27""数据27.2""数据27.3""数据27.4""数据27.5" });   
  125.         list.add(new String[] { "28""数据28.2""数据28.3""数据28.4""数据28.5" });   
  126.         list.add(new String[] { "29""数据29.2""数据29.3""数据29.4""数据29.5" });   
  127.         list.add(new String[] { "30""数据12.2""数据12.3""数据12.4""数据12.5" });   
  128.         list.add(new String[] { "31""数据13.2""数据13.3""数据13.4""数据13.5" });   
  129.         list.add(new String[] { "32""数据14.2""数据14.3""数据14.4""数据14.5" });   
  130.         list.add(new String[] { "33""数据15.2""数据15.3""数据15.4""数据15.5" });   
  131.         list.add(new String[] { "34""数据16.2""数据16.3""数据16.4""数据16.5" });   
  132.         list.add(new String[] { "35""数据17.2""数据17.3""数据17.4""数据17.5" });   
  133.         list.add(new String[] { "36""数据18.2""数据18.3" });   
  134.         list.add(new String[] { "37""数据19.2""数据19.3""数据19.4""数据19.5" });   
  135.         list.add(new String[] { "38""数据20.2""数据20.3""数据20.4""数据20.5" });   
  136.         list.add(new String[] { "39""数据21.2""数据21.3""数据21.4""数据21.5" });   
  137.         list.add(new String[] { "40""数据22.2""数据22.3""数据22.4""数据22.5" });   
  138.         list.add(new String[] { "41""数据23.2""数据23.3""数据23.4""数据23.5" });   
  139.         list.add(new String[] { "42""数据24.2""数据24.3""数据24.4""数据24.5" });   
  140.         list.add(new String[] { "43""数据25.2""数据25.3""数据25.4""数据25.5" });   
  141.         list.add(new String[] { "44""数据26.2""数据26.3""数据26.4""数据26.5" });   
  142.         list.add(new String[] { "45""数据27.2""数据27.3""数据27.4""数据27.5" });   
  143.         list.add(new String[] { "46""数据28.2""数据28.3""数据28.4""数据28.5" });   
  144.         list.add(new String[] { "47""数据29.2""数据29.3""数据29.4""数据29.5" });   
  145.     }   
  146.   
  147. }  
package com.iaiai;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

/**
 * 
 * <p>
 * Title: MainActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-9
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class MainActivity extends Activity {

	TableView listView;
	List<String[]> list = new ArrayList<String[]>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		makeData();

		String[] title = new String[] { "ID", "标题2", "标题3", "标题4", "标题5" };

		listView = new TableView(this, title, list);
		listView.setTitleTextColor(Color.GREEN);
		listView.setTitleBackgroundColor(Color.RED);
		// listView.setAutoColumnWidth(3);
		// listView.setItemBackgroundColor(Color.GREEN, Color.WHITE);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String[] item = listView.getItem(position);
				item[2] = "12312312312";
				listView.definedSetChanged();
			}
		});

		// listView.definedSetChanged();

		setContentView(listView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "测试修改选中行");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			listView.setSelectedPosition(32);
			break;
		}
		return true;
	}

	private void makeData() {
		list.add(new String[] { "1", "数据1.2", "数据1.3", "数据1.4" });
		list.add(new String[] { "2", "数据2.2", "数据2.3", "数据2.4", "数据2.5" });
		list.add(new String[] { "3", "数据3.2", "数据3.3", "数据3.4" });
		list.add(new String[] { "4", "数据4.2", "数据4.3", "数据4.4" });
		list.add(new String[] { "5", "数据5.2", "数据5.3", "数据5.4", "数据5.5" });
		list.add(new String[] { "6", "数据6.2", "数据6.3", "数据6.4" });
		list.add(new String[] { "7", "数据7.2", "数据7.3", "数据7.4", "数据7.5",
				"数据7.6" });
		list.add(new String[] { "8", "数据8.2", "数据8.3", "数据8.4" });
		list.add(new String[] { "9", "数据9.2", "数据9.3", "数据9.4" });
		list.add(new String[] { "10", "数据10.2", "数据10.3", "数据10.4", "数据10.5",
				"数据10.6", "数据10.7" });
		list.add(new String[] { "11", "数据11.2", "数据11.3", "数据11.4", "数据11.5" });
		list.add(new String[] { "12", "数据12.2", "数据12.3", "数据12.4", "数据12.5" });
		list.add(new String[] { "13", "数据13.2", "数据13.3", "数据13.4", "数据13.5" });
		list.add(new String[] { "14", "数据14.2", "数据14.3", "数据14.4", "数据14.5" });
		list.add(new String[] { "15", "数据15.2", "数据15.3", "数据15.4", "数据15.5" });
		list.add(new String[] { "16", "数据16.2", "数据16.3", "数据16.4", "数据16.5" });
		list.add(new String[] { "17", "数据17.2", "数据17.3", "数据17.4", "数据17.5" });
		list.add(new String[] { "18", "数据18.2", "数据18.3", "数据18.4", "数据18.5" });
		list.add(new String[] { "19", "数据19.2", "数据19.3", "数据19.4", "数据19.5" });
		list.add(new String[] { "20", "数据20.2", "数据20.3", "数据20.4", "数据20.5" });
		list.add(new String[] { "21", "数据21.2", "数据21.3", "数据21.4", "数据21.5" });
		list.add(new String[] { "22", "数据22.2", "数据22.3", "数据22.4", "数据22.5" });
		list.add(new String[] { "23", "数据23.2", "数据23.3", "数据23.4", "数据23.5" });
		list.add(new String[] { "24", "数据24.2", "数据24.3", "数据24.4", "数据24.5" });
		list.add(new String[] { "25", "数据25.2", "数据25.3", "数据25.4", "数据25.5" });
		list.add(new String[] { "26", "数据26.2" });
		list.add(new String[] { "27", "数据27.2", "数据27.3", "数据27.4", "数据27.5" });
		list.add(new String[] { "28", "数据28.2", "数据28.3", "数据28.4", "数据28.5" });
		list.add(new String[] { "29", "数据29.2", "数据29.3", "数据29.4", "数据29.5" });
		list.add(new String[] { "30", "数据12.2", "数据12.3", "数据12.4", "数据12.5" });
		list.add(new String[] { "31", "数据13.2", "数据13.3", "数据13.4", "数据13.5" });
		list.add(new String[] { "32", "数据14.2", "数据14.3", "数据14.4", "数据14.5" });
		list.add(new String[] { "33", "数据15.2", "数据15.3", "数据15.4", "数据15.5" });
		list.add(new String[] { "34", "数据16.2", "数据16.3", "数据16.4", "数据16.5" });
		list.add(new String[] { "35", "数据17.2", "数据17.3", "数据17.4", "数据17.5" });
		list.add(new String[] { "36", "数据18.2", "数据18.3" });
		list.add(new String[] { "37", "数据19.2", "数据19.3", "数据19.4", "数据19.5" });
		list.add(new String[] { "38", "数据20.2", "数据20.3", "数据20.4", "数据20.5" });
		list.add(new String[] { "39", "数据21.2", "数据21.3", "数据21.4", "数据21.5" });
		list.add(new String[] { "40", "数据22.2", "数据22.3", "数据22.4", "数据22.5" });
		list.add(new String[] { "41", "数据23.2", "数据23.3", "数据23.4", "数据23.5" });
		list.add(new String[] { "42", "数据24.2", "数据24.3", "数据24.4", "数据24.5" });
		list.add(new String[] { "43", "数据25.2", "数据25.3", "数据25.4", "数据25.5" });
		list.add(new String[] { "44", "数据26.2", "数据26.3", "数据26.4", "数据26.5" });
		list.add(new String[] { "45", "数据27.2", "数据27.3", "数据27.4", "数据27.5" });
		list.add(new String[] { "46", "数据28.2", "数据28.3", "数据28.4", "数据28.5" });
		list.add(new String[] { "47", "数据29.2", "数据29.3", "数据29.4", "数据29.5" });
	}

}



我把整个功能写成了一个类,在需要用的时候只需要new出这个类就可以直接使用了
该类目前的所有方法:
Java代码 复制代码  收藏代码
  1. /** 初始化带标题ListView. */  
  2. CListView(Context, String[], List<String[]>)   
  3. /** 整体有改变时,刷新显示. */  
  4. definedSetChanged()   
  5. /** 设置选中时的监听器. */  
  6. setOnItemClickListener(OnItemClickListener)   
  7. /** 设置行背景颜色, 多个颜色可以作为间隔色. */  
  8. setItemBackgroundColor(int...)   
  9. /** 数据总数. */  
  10. getCount()   
  11. /** 当前选中数据. */  
  12. getItem(int)   
  13. /** 设置当前选中位置. */  
  14. setSelectedPosition(int)   
  15. /** 当前选中位置. */  
  16. getSelectedPosition()   
  17. /** 设置被选中时的背景色. */  
  18. setSelectedBackgroundColor(int)   
  19. /** 设置标题背景色. */  
  20. setTitleBackgroundColor(int)   
  21. /** 设置标题文字颜色. */  
  22. setTitleTextColor(int)   
  23. /** 设置内容文字颜色. */  
  24. setContentTextColor(int)   
  25. /** 设置标题字体大小. */  
  26. setTitleTextSize(float)   
  27. /** 设置内容字体大小. */  
  28. setContentTextSize(float)   
  29. /** 设定哪列自动列宽 从0开始计算. */  
  30. setAutoColumnWidth(int)  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值