android 用ListvVew实现表格功能Sample
2013-07-14 16:57
429 查看
前言
ListvVew是很常用的组件,组要在于它的list功能,而且可扩展性强,可以自定义布局方式,这就使得我们可以用它,制定特殊的布局方式制作表格。而且还可以改变不同的列宽。这是它区别与GridbView制作的表格的不同的特性。
注:本文demo源码下载地址:http://download.csdn.net/detail/dxzysk/5752157
一、效果图
Demo制作的表格如下图所示:
界面不是很漂亮,大家按照需要修改一下
二、部分代码
ListvVew是很常用的组件,组要在于它的list功能,而且可扩展性强,可以自定义布局方式,这就使得我们可以用它,制定特殊的布局方式制作表格。而且还可以改变不同的列宽。这是它区别与GridbView制作的表格的不同的特性。
注:本文demo源码下载地址:http://download.csdn.net/detail/dxzysk/5752157
一、效果图
Demo制作的表格如下图所示:
界面不是很漂亮,大家按照需要修改一下
二、部分代码
package nishik.ncc.configurator; import java.util.ArrayList; import java.util.HashMap; import nishik.ncc.configurator.TableAdapter; import nishik.ncc.configurator.TableAdapter.TableEachRow; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.util.Log; import android.view.GestureDetector; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class ListviewTableActivity extends Activity implements OnItemClickListener{ String mIndex[]; String mItem[]; final int ONE_PAGE_COLUMN_COUNT = 6; final int ONE_PAGE_COLUMN_STABLE = 2; final int ONE_PAGE_COLUMN_BRANCH = 3;//分支每一页显示的轴或列数 private int mStartNo=0;// 轴或系统号,从0开始标记 private ListView mparalistview; private ArrayList<String> mstrHeaderNames; GestureDetector mDetector; ArrayList<Integer> m_titleWidths = new ArrayList<Integer>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview_test); mparalistview = (ListView) findViewById(R.id.ParaListView); //setTestlistview(); setoldlistview(); setScrolListview(); } void setTestlistview(){ int width = this.getWindowManager().getDefaultDisplay().getWidth()/ONE_PAGE_COLUMN_COUNT; int[] titleWidths = {width, width*2, width, width, width,width,width,width,width,width}; setTitleWidth(titleWidths); mIndex = getResources().getStringArray(R.array.index); mItem = getResources().getStringArray(R.array.item); // mparalistview.setAdapter(getAdapter(mIndex, mItem)); LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mparalistview.setAdapter(new listnccAdapter(mInflater)); mparalistview.setOnItemClickListener(this); } void setoldlistview(){ String [] strHeaderNames = { "番号","名称","X","Y","Z","A","B","C","U","V"}; int width = this.getWindowManager().getDefaultDisplay().getWidth()/ONE_PAGE_COLUMN_COUNT; int[] titleWidths = {width, width*2, width, width, width,width,width,width,width,width}; setTitleWidth(titleWidths); setGridHeader(strHeaderNames, titleWidths); setGridContent(); } class listnccAdapter extends BaseAdapter{ private LayoutInflater mInflater; listnccAdapter(LayoutInflater inflate){ this.mInflater = inflate; } public int getCount() { // TODO Auto-generated method stub return 40; } public Object getItem(int position) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // if (convertView == null) { // // convertView = mInflater.inflate(R.layout.listitemview, null); // // } // 每行的数据 TableCell[] cells = new TableCell[ONE_PAGE_COLUMN_STABLE+ONE_PAGE_COLUMN_BRANCH]; cells[0] = new TableCell(String.valueOf(NccDemoCommonData.subAxisAxisNo[0]), m_titleWidths.get(0), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.rgb(200,255,255)); cells[1] = new TableCell(String.valueOf(NccDemoCommonData.subAxisAxisName[0]), m_titleWidths.get(1), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.rgb(200,255,255)); mStartNo = 0; int startNo = ONE_PAGE_COLUMN_STABLE+mStartNo; for(int j=startNo; j < ONE_PAGE_COLUMN_STABLE+ONE_PAGE_COLUMN_BRANCH; j++){ cells[j] = new TableCell("000"+(j-startNo), m_titleWidths.get(j), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.WHITE); } //table.add(new TableEachRow(cells)); TableEachRow tableRow = new TableEachRow(cells);//table.get(position); if ((convertView != null) &&(convertView instanceof TableRowView)) { ((TableRowView)convertView).setRowDisplay(tableRow, position); //Log.i("row", "A new row " + position); //tableRow.diplayColor(); } else { convertView = new TableRowView(ListviewTableActivity.this, tableRow,position); } return convertView; } } public void setTitleWidth(int[] titleWidths){ for (int iIndex = 0; iIndex < titleWidths.length; iIndex++){ m_titleWidths.add(Integer.valueOf(titleWidths[iIndex])); } } public void setGridHeader(String[] titleNames, int[] titleWidths){ mstrHeaderNames = new ArrayList<String>(); for(int index =0;index < titleNames.length;index++){ mstrHeaderNames.add(titleNames[index]); } LinearLayout llHeaderLayout = (LinearLayout) this.findViewById(R.id.table_headerlayout); llHeaderLayout.setBackgroundColor(Color.LTGRAY); for(int iIndex = 0; iIndex < titleNames.length;iIndex++){ LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( titleWidths[iIndex], android.view.ViewGroup.LayoutParams.WRAP_CONTENT); TextView textCell = new TextView(ListviewTableActivity.this); textCell.setGravity(Gravity.LEFT); textCell.setBackgroundColor(/*Color.LTGRAY*/Color.rgb(0xa0, 0xa0, 0xa0)); textCell.setText(titleNames[iIndex]); textCell.setTextColor(Color.BLACK); textCell.setSingleLine(); if (0==iIndex){ layoutParams.setMargins(0, 2, 0, 0); }else{ layoutParams.setMargins(2, 2, 0, 0); } llHeaderLayout.addView(textCell, layoutParams); } } public void setGridContent(){ if (null == mparalistview){ mparalistview = (ListView) this.findViewById(R.id.ParaListView); } mparalistview.setBackgroundColor(Color.LTGRAY); ArrayList<TableAdapter.TableEachRow> table = new ArrayList<TableAdapter.TableEachRow>(); for (int i = 0; i < 25; i++) { // 每行的数据 TableCell[] cells = new TableCell[ONE_PAGE_COLUMN_STABLE+ONE_PAGE_COLUMN_BRANCH]; cells[0] = new TableCell(String.valueOf(NccDemoCommonData.subAxisAxisNo[i]), m_titleWidths.get(0), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.rgb(200,255,255)); cells[1] = new TableCell(String.valueOf(NccDemoCommonData.subAxisAxisName[i]), m_titleWidths.get(1), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.rgb(200,255,255)); mStartNo = 0; int startNo = ONE_PAGE_COLUMN_STABLE+mStartNo; for(int j=startNo; j < ONE_PAGE_COLUMN_STABLE+ONE_PAGE_COLUMN_BRANCH; j++){ cells[j] = new TableCell("000"+(j-startNo), m_titleWidths.get(j), android.view.ViewGroup.LayoutParams.FILL_PARENT, TableCell.STRING, Color.WHITE); } table.add(new TableEachRow(cells)); } TableAdapter tableAdapter = new TableAdapter(this, table); mparalistview.setAdapter(tableAdapter); //mparalistview.setOnItemClickListener(new ItemClickEvent()); } public void UpdateGridHeader(){ LinearLayout llHeaderLayout = (LinearLayout) this.findViewById(R.id.table_headerlayout); int startindex = this.mStartNo+ONE_PAGE_COLUMN_STABLE; for(int index = ONE_PAGE_COLUMN_STABLE;index < llHeaderLayout.getChildCount();index++){ View vItem = llHeaderLayout.getChildAt(index); if(vItem instanceof TextView){ int headerindex = startindex + index - ONE_PAGE_COLUMN_STABLE; if(headerindex < mstrHeaderNames.size()){ ((TextView)vItem).setText(mstrHeaderNames.get(headerindex)); //Log.i("info","SetText = "+ mstrHeaderNames.get(headerindex)); }else{ ((TextView)vItem).setText(""); //Log.i("info","SetText = null"); } } vItem.invalidate(); } // for(int iIndex = 0; iIndex < this.mparalistview.getCount();iIndex++){ // View vItem = this.mparalistview.getChildAt(iIndex); // if(vItem instanceof ) // } } public void UpdateGridContent(){ //int rowcount = mparalistview.getCount(); //Log.i("info", "roowcount="+rowcount); ListAdapter la = mparalistview.getAdapter(); if (false == (la instanceof TableAdapter)){ return ; } TableAdapter ta = (TableAdapter) la; ArrayList<TableEachRow> tables = ta.GetTable(); int rowcount = tables.size(); Log.i("info", "roowcount="+rowcount); SelectCell selCell = ta.GetSelCell(); for (int index = 0; index < rowcount; index++) { TableEachRow ter = tables.get(index); //Log.i("info", "row="+index); //int startindex = this.mStartNo+ONE_PAGE_COLUMN_STABLE; for(int indexcol = ONE_PAGE_COLUMN_STABLE;indexcol < ter.getSize();indexcol++){ TableCell tableCell = ter.getCellValue(indexcol); int branchindex = this.mStartNo+indexcol - this.ONE_PAGE_COLUMN_STABLE; //Log.i("info", "col="+indexcol); if (tableCell.getType() == TableCell.STRING) { int colindex = branchindex+ONE_PAGE_COLUMN_STABLE; if(colindex < mstrHeaderNames.size()){ tableCell.setCellValue("000"+branchindex); }else{ tableCell.setCellValue(""); } Log.i("info", "Updated value is "+String.valueOf(tableCell.value)); tableCell.setCellColor(Color.WHITE); TextView textCellview = (TextView)tableCell.getTextView(); if (null != textCellview){ textCellview.setText(String.valueOf(tableCell.value)); textCellview.setBackgroundColor(Color.WHITE);// } if (null != selCell){ if((textCellview == selCell.tbCell.cellview)&&(textCellview!=null)){ textCellview.setBackgroundColor(selCell.cellcolor); tableCell.setCellColor(selCell.cellcolor); } } tableCell.setTextView(null); //tableCell.setTextView(textCell); //v.invalidate(); } } } Log.i("info", "ta.notifyDataSetChanged() is runned"); ta.notifyDataSetChanged(); } public void setScrolListview(){ if(null == mparalistview){ return ; } mDetector =new GestureDetector(this, new listViewItemOnGestureListener()); mparalistview.setOnTouchListener(new ListView.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub //Log.i("info","ListView.OnTouchListener:onTouch is calledd."); //GestureDetector mDetector = new GestureDetector(new GestureDetector.OnGestureListener(){ return mDetector.onTouchEvent(event); } }); } class listViewItemOnGestureListener implements GestureDetector.OnGestureListener{ final int FLIP_MIN_DISTANCE = 30; final int FLIP_THRESHOLD_VELOCITY = 200; public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { // TODO Auto-generated method stub Log.i("info","GestureDetector:onFling is calledd."); //Log.i("info","velocityX="+velocityX); if(null == event1){ Log.i("info","event1 is null "); return false; } if(null == event2){ //Log.i("info","event2 is null "); return false; } Log.i("info","event1.getX()="+event1.getX()+"event2.getX()"+event2.getX()); /* * 如果第一个触点事件的X座标大于第二个触点事件的X座标超过FLIP_DISTANCE * 也就是手势从右向左滑。 */ if ((event1.getX() - event2.getX() > FLIP_MIN_DISTANCE) && (Math.abs(velocityX) > FLIP_THRESHOLD_VELOCITY)) { Log.i("info","touch from right to the left"); //Toast.makeText(NCDataDisplay.this, "touch from right to the left", Toast.LENGTH_SHORT).show(); if( (ListviewTableActivity.this.mStartNo + ONE_PAGE_COLUMN_BRANCH) < (mstrHeaderNames.size() - ONE_PAGE_COLUMN_STABLE - 1)){ ListviewTableActivity.this.mStartNo = ListviewTableActivity.this.mStartNo + ONE_PAGE_COLUMN_BRANCH; UpdateGridHeader(); UpdateGridContent(); } return true; } /* * 如果第二个触点事件的X座标大于第一个触点事件的X座标超过FLIP_DISTANCE * 也就是手势从左向右滑。 */ else if ((event2.getX() - event1.getX() > FLIP_MIN_DISTANCE) && (Math.abs(velocityX) > FLIP_THRESHOLD_VELOCITY)) { Log.i("info","touch from left to right "); //Toast.makeText(NCDataDisplay.this, "touch from left to right ", Toast.LENGTH_SHORT).show(); if(ListviewTableActivity.this.mStartNo - ONE_PAGE_COLUMN_BRANCH >= 0){ ListviewTableActivity.this.mStartNo -= ONE_PAGE_COLUMN_BRANCH; UpdateGridHeader(); UpdateGridContent(); } return true; } return false; } public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } } private SimpleAdapter getAdapter(String[] indexArray, String[] itemArray) { ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < indexArray.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("index", indexArray[i]); map.put("itemText1", itemArray[i]); map.put("itemText2", itemArray[i]); data.add(map); } SimpleAdapter simperAdapter = new SimpleAdapter(this, data, R.layout.listitemview, new String[] { "index", "itemText1", "itemText2" }, new int[] { R.id.textView1, R.id.textView2 , R.id.textView3}); return simperAdapter; } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_listview_test, menu); return true; } } class TableAdapter extends BaseAdapter { private Context context; private ArrayList<TableEachRow> table; private SelectCell selCell; public TableAdapter(Context context, ArrayList<TableEachRow> table2) { this.context = context; this.table = table2; } public SelectCell GetSelCell(){ return selCell; } public ArrayList<TableEachRow> GetTable(){ return table; } public int getCount() { return table.size(); } public long getItemId(int position) { return position; } public TableEachRow getItem(int position) { return table.get(position); } public View getView(int position, View convertView, ViewGroup parent) { // TableEachRow tableRow = table.get(position); // TableRowView trv = new TableRowView(this.context, tableRow,position); // return trv; TableEachRow tableRow = table.get(position); if ((convertView != null) &&(convertView instanceof TableRowView)) { ((TableRowView)convertView).setRowDisplay(tableRow, position); //Log.i("row", "A new row " + position); //tableRow.diplayColor(); } else { convertView = new TableRowView(this.context, tableRow,position); } //GridCell gc = (GridCell)selCell.tbCell.cellview.getTag(); // Log.i("select","row"+gc.row+"col"+gc.col); //Log.i("select","A new row started row:" + position); //tableRow.diplayColor(); tableRow.diplayValue(); return convertView; } /** * TableRow 实现表格的行 * @author hellogv */ static public class TableEachRow { private TableCell[] cell; public TableEachRow(TableCell[] cell) { this.cell = cell; } public int getSize() { return cell.length; } public TableCell getCellValue(int index) { if (index >= cell.length) return null; return cell[index]; } public Object getCellValueof(int col){ return cell[col].getCellValue(); } public void diplayColor(){ for (int i=0; i<getSize();i++){ Log.i("color","color"+ i+ "="+cell[i].getCellColor()); } } public void diplayValue(){ for (int i=0; i<getSize();i++){ Log.i("color","value"+ i+ "="+String.valueOf(cell[i].getCellValue())); } } } } /** * TableRowView 实现表格行的样式 * @author hellogv */ class TableRowView extends LinearLayout { private Context context; private int rowIndex; private SelectCell selCell; public TableRowView(Context context, TableEachRow tableRow, int index) { super(context); this.context = context; this.rowIndex = index; this.setOrientation(LinearLayout.HORIZONTAL); //this.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); this.setLayoutParams(new GridView.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); this.setBackgroundColor(Color.LTGRAY); this.setFocusable(false); this.setFocusableInTouchMode(false); LayoutInflater Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout.LayoutParams innerlayoutParams = new LinearLayout.LayoutParams( android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); View innerview = Inflater.inflate(R.layout.newlistitemview, null); ViewGroup innerviewGroup = null; if(innerview instanceof ViewGroup){ innerviewGroup = (ViewGroup)innerview; Log.i("info","innerview has got the right value"); }else{ Log.i("info","error:innerview is null"); return; } Log.i("info","innerviewGroup.getChildCount="+innerviewGroup.getChildCount()); addView(innerview, innerlayoutParams); for (int i = 0; i < innerviewGroup.getChildCount(); i++) { TableCell tableCell = tableRow.getCellValue(i); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( tableCell.width, tableCell.height); if (0==i){ layoutParams.setMargins(0, 1, 0, 0); }else{ layoutParams.setMargins(2, 1, 0, 0); } if (tableCell.type == TableCell.STRING) { //TextView textCell = new TextView(context); TextView textCell = (TextView) innerviewGroup.getChildAt(i); //textCell.setTextSize(20); textCell.setLayoutParams(layoutParams); textCell.setGravity(Gravity.LEFT); textCell.setBackgroundColor(tableCell.color);// textCell.setText(String.valueOf(tableCell.value)); textCell.setSingleLine(); textCell.setSelectAllOnFocus(true); textCell.setTag(new GridCell(rowIndex, i, tableCell.color)); //textCell.setTag(1, Integer.valueOf(i)); textCell.setOnClickListener(new CellClickListener(tableCell)); textCell.setFocusable(false); textCell.setFocusableInTouchMode(false); //addView(textCell, layoutParams); } else if (tableCell.type == TableCell.IMAGE) { ImageView imgCell = new ImageView(context); imgCell.setBackgroundColor(Color.BLACK); imgCell.setImageResource((Integer) tableCell.value); imgCell.setFocusable(false); imgCell.setFocusableInTouchMode(false); addView(imgCell, layoutParams); } } } class CellClickListener implements View.OnClickListener { private TableCell m_tableCell; CellClickListener(TableCell tableCell){ m_tableCell = tableCell; } public void onClick(View v) { // TODO Auto-generated method stub // [return to its former color] } } void setRowDisplay(TableEachRow tableRow, int index){ this.rowIndex = index; for (int i = 0; i < tableRow.getSize(); i++) { TableCell tableCell = tableRow.getCellValue(i); if (tableCell.type == TableCell.STRING) { View v = this.getChildAt(i); if (v instanceof TextView){ tableCell.cellview = null; TextView textCell = (TextView)v; textCell.setText(String.valueOf(tableCell.value)); textCell.setBackgroundColor(tableCell.color);// tableCell.setTextView(textCell); textCell.setOnClickListener(new CellClickListener(tableCell)); textCell.setTag(new GridCell(rowIndex, i, tableCell.color));; tableCell.setCellColor(Color.WHITE); } } } } } /** * TableCell 实现表格的格单元 * @author hellogv */ class TableCell { static public final int STRING = 0; static public final int IMAGE = 1; public Object value; public int width; public int height; public int color; public int type; public View cellview; public TableCell(Object value, int width, int height, int type, int color) { this.value = value; this.width = width; this.height = height; this.type = type; this.color = color; } public void setCellColor(int clor){ this.color = clor; } public Object getCellValue(){ return value; } public void setCellValue(String str){ this.value = str; } public int getCellColor(){ return this.color; } public void setTextView(View v){ this.cellview = v; } public View getTextView(){ return this.cellview; } public int getType(){ return type; } } class GridCell { int row; int col; int color; GridCell(int row, int col, int color){ this.row = row; this.col = col; this.color = color; } } class SelectCell{ int cellcolor; TableCell tbCell; SelectCell(int color,TableCell tbcell){ this.cellcolor = color; this.tbCell = tbcell; } }
相关文章推荐
- android 用GridVew实现表格功能Sample
- Android (ListActivity、Contextmenu)微信类消息置顶和删除和功能的实现
- 自定义搜索功能Android实现
- Android 实现WebView点击图片查看大图列表及图片保存功能
- android实现截屏功能代码
- Android 仿微信实现语音聊天功能
- Android 5.0及以上编程实现屏幕截图功能的方法
- Android 微信摇一摇功能实现详细介绍
- 足迹二:Android客户端与PC服务端之间的SOCKET通讯实现登陆功能(客户端 仅供学习)
- 实例讲解Android中如何实现图片的异步加载功能
- Android 端“被挤下线”功能的实现
- android 实现手电筒基本功能
- Android实现短信验证功能(功能的使用)
- Android—强制下线功能的实现
- Android SharedPreferences工具类 ,实现List/Map的保存读取
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android实现对图片放大、平移和旋转的功能
- android简单实现页面滑动关闭功能
- Android 实现头像上传功能
- JS中兼容实现android和ios系统手机打开相机并可选择相册功能