Android中使用ListView繪製自訂表格格(2),androidlistview
上回再寫了《Android中使用ListView繪製自訂表格格》後,很多人留言代碼不全和沒有資料範例。但因為項目原因,沒法把源碼全部貼上來。近兩天,抽空簡化了一下,做了一個例子。
如
一、功能:
1、支援列合并
2、考慮了介面重新整理最佳化
3、預留部分介面
4、支援左右滾動
1、枚舉類:CellTypeEnum
package csdn.danielinbiti.custometableview.item;public enum CellTypeEnum { STRING //字元 ,DIGIT //數字 ,LABEL //標籤}該類定義了表格支援的樣式,可以根據需要擴充,擴充了新類型,注意同時修改CustomeTableItem中新類型樣式的建立方式
2、核心代碼CustomeTableItem,該類對應ListView的一行item。類支援列合并,沒有實現行合并(行合并樣式控制上會比較複雜些,如有需要自己改寫吧)
rowtype:該值主要表示表格中不同行的樣式,如果合并的列都一樣的行,則可以複用,不需要再建立了。
package csdn.danielinbiti.custometableview.item;import java.util.ArrayList;import csdn.danielinbiti.custometableview.R;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.LinearLayout.LayoutParams;public class CustomeTableItem extends LinearLayout {private Context context = null;private boolean isRead = false;//是否唯讀private ArrayList<View> viewList = new ArrayList();//行的表格列表private int[] headWidthArr = null;//表頭的列寬設定private String rowType = "0";//行的樣式idpublic CustomeTableItem(Context context) {super(context);}public CustomeTableItem(Context context, AttributeSet attrs) {super(context, attrs);}public CustomeTableItem(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}/* * rowType:行的樣式,字元任意,相同樣式的行不需要再建立了 * itemCells:儲存格資訊 * headWidthArr:每列寬度 * isRead:是否唯讀,如果是唯讀,則所有的輸入都不生效 */ public void buildItem(Context context,String rowType,ArrayList<ItemCell> itemCells ,int[] headWidthArr,boolean isRead){this.setOrientation(LinearLayout.VERTICAL);//第一層布局垂直 this.context = context; this.headWidthArr =headWidthArr.clone(); this.rowType = rowType; this.addCell(itemCells); } private void addCell(ArrayList<ItemCell> itemCells){ this.removeAllViews(); LinearLayout secondLayout = new LinearLayout(context);secondLayout.setOrientation(LinearLayout.HORIZONTAL);secondLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));this.addView(secondLayout); int cellIndex = 0; for(int i=0;i<itemCells.size();i++){ ItemCell itemCell = itemCells.get(i);int endIndex = cellIndex+itemCell.getCellSpan();//所佔行數int width = getCellWidth(cellIndex,endIndex);//行寬度cellIndex = endIndex; if(itemCell.getCellType()==CellTypeEnum.STRING){ EditText view= (EditText)getInputView();view.setText(itemCell.getCellValue());view.setWidth(width);this.setEditView(view);secondLayout.addView(view);viewList.add(view); }else if(itemCell.getCellType()==CellTypeEnum.DIGIT){ EditText view= (EditText)getInputView();view.setText(itemCell.getCellValue());view.setWidth(width);this.setEditView(view);this.setOnKeyBorad(view);secondLayout.addView(view);viewList.add(view); }else if(itemCell.getCellType()==CellTypeEnum.LABEL){ TextView view = (TextView)getLabelView();view.setText(itemCell.getCellValue());view.setWidth(width);secondLayout.addView(view);viewList.add(view); } if(i!=itemCells.size()-1){//插入豎線LinearLayout v_line = (LinearLayout)getVerticalLine();v_line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));secondLayout.addView(v_line);} } } public void refreshData(ArrayList<ItemCell> itemCells){for(int i=0;i<itemCells.size();i++){ItemCell itemCell = itemCells.get(i);if(itemCell.getCellType()==CellTypeEnum.LABEL){TextView view = (TextView)viewList.get(i);view.setText(itemCell.getCellValue());}else if(itemCell.getCellType()==CellTypeEnum.DIGIT){EditText view= (EditText)viewList.get(i);view.setText(itemCell.getCellValue());this.setEditView(view);this.setOnKeyBorad(view);}else if(itemCell.getCellType()==CellTypeEnum.STRING){EditText view= (EditText)viewList.get(i);view.setText(itemCell.getCellValue());this.setEditView(view);}}} private View getVerticalLine(){return LayoutInflater.from(context).inflate(R.layout.atom_line_v_view, null);} private int getCellWidth(int cellStart,int cellEnd){int width = 0;for(int i=cellStart;i<cellEnd;i++){width = this.headWidthArr[i] + width;}return width;} private View getLabelView(){return (View)LayoutInflater.from(context).inflate(R.layout.atom_text_view, null);}private View getInputView(){return (View)LayoutInflater.from(context).inflate(R.layout.atom_edttxt_view, null);}private void setEditView(EditText edtText1){ if(this.isRead){ edtText1.setEnabled(false); }else{ }}private void setOnKeyBorad(EditText edtText1){//數字鍵台if(!this.isRead){//非唯讀}}public String getRowType() {return rowType;}}
源碼點擊開啟連結
跪怎使用ListView繪製表格
可以啊 ListView 每個item對應表格的一行,設計一個LinearLayout(水平方向),裡面設計多列TextView,加一些邊框線等效果然後在adapter中的getView()返回linearLayout就可以了
對於android自訂的listview裡有togglebutton同時使用的問題,解答
因為listview裡面的view是重複使用的,也就是當一個頁面中,
如果所見的列表view對象已經建立了,你沒有作特殊處理的話,那麼這些view就會在之後的清單項目中被重複引用,
使用listview就是為了達到重複使用模版view的作用,佔用最少的記憶體,達到最快的速度。
你的情況有兩種做法
第一、修改listview的adapter,對getview方法進行特殊處理(要求你要很好的理解listview的原理)
第二、使用scrollview,最省事最直接