Android 自定义DataGridView数据表格控件
我是一个.net程序员,但是苦于公司要求开发一个android app,没办法,只能硬着头皮上了。
由于项目里面很多地方需要用到数据显示控件(类似于.net的DataGridView),度娘找了下发现没人公开类似的控件,没办法只好自己写了。
废话不多说,直接贴代码:
1 public class DataGridView extends HorizontalScrollView { 2 private List<DataGridViewColumn> columns; 3 private List<Map<String,String>> rows; 4 5 private boolean hasHeader; 6 7 private CellClickListener cellClickListener; 8 private RowClickListener rowClickListener; 9 private RowValidatorListener rowValidatorListener; 10 private LinearLayout headerRow; 11 private LinearLayout bodyRow; 12 13 public DataGridView(Context context, AttributeSet attrs) { 14 super(context, attrs); 15 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DataGridView); 16 hasHeader = a.getBoolean(R.styleable.DataGridView_hasHeader, true); 17 a.recycle(); 18 19 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 20 LinearLayout container = (LinearLayout) inflater.inflate(R.layout.ctrl_data_grid_view, null, false); 21 addView(container); 22 this.columns = new ArrayList<DataGridViewColumn>(); 23 this.rows = new ArrayList<Map<String,String>>(); 24 headerRow = new LinearLayout(getContext()); 25 headerRow.setOrientation(LinearLayout.HORIZONTAL); 26 headerRow.setBackgroundResource(R.drawable.datagrid_header_background); 27 headerRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 28 if (hasHeader){ 29 container.addView(headerRow); 30 } 31 ScrollView scrollView = (ScrollView)inflater.inflate(R.layout.ctrl_data_grid_view_scroll, container, false); 32 bodyRow = (LinearLayout) inflater.inflate(R.layout.ctrl_data_grid_view, scrollView, false); 33 scrollView.addView(bodyRow); 34 container.addView(scrollView); 35 } 36 37 public void addColumn(String dataField, String headerText){ 38 this.addColumn(dataField, headerText, 200); 39 } 40 41 public void addColumn(String dataField, String headerText, int columnWidth){ 42 this.addColumn(dataField, headerText, columnWidth, Gravity.START); 43 } 44 45 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign){ 46 this.addColumn(dataField, headerText, columnWidth, textAlign, Color.rgb(80, 80, 80)); 47 } 48 49 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign, int textColor){ 50 this.addColumn(dataField, headerText, columnWidth, textAlign, textColor, true); 51 } 52 53 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign, int textColor, boolean isEnabled){ 54 DataGridViewColumn column = new DataGridViewColumn(); 55 column.dataField =dataField; 56 column.headerText = headerText; 57 column.columnWidth = columnWidth; 58 column.textAlign = textAlign; 59 column.textColor = textColor; 60 column.isEnabled = isEnabled; 61 this.addColumn(column); 62 } 63 64 public void addColumn(DataGridViewColumn column){ 65 columns.add(column); 66 insertColumn(column); 67 if (rows.size() > 0){ 68 bodyRow.removeAllViews(); 69 for (Map<String,String> row : rows){ 70 insertRow(row); 71 } 72 } 73 } 74 75 public void addColumn(DataGridViewColumn column, int index){ 76 columns.add(column); 77 insertColumn(column, index); 78 if (rows.size() > 0){ 79 bodyRow.removeAllViews(); 80 for (Map<String,String> row : rows){ 81 insertRow(row); 82 } 83 } 84 } 85 86 public void removeColumn(int index){ 87 columns.remove(index); 88 } 89 90 public void removeColumn(String dataField){ 91 for (DataGridViewColumn column : columns){ 92 if (column.dataField.equals(dataField)){ 93 this.removeColumn(column); 94 if (rows.size() > 0){ 95 bodyRow.removeAllViews(); 96 for (Map<String,String> row : rows){ 97 insertRow(row); 98 } 99 } 100 return; 101 } 102 } 103 } 104 105 public void removeColumn(DataGridViewColumn column){ 106 columns.remove(column); 107 } 108 109 public void setDataSource(List<Map<String,String>> rows){ 110 this.rows = rows; 111 if (columns.size() > 0){ 112 bodyRow.removeAllViews(); 113 for (Map<String,String> row : rows){ 114 insertRow(row); 115 } 116 } 117 } 118 119 public void addRow(Map<String,String> row){ 120 rows.add(row); 121 if (columns.size() > 0) { 122 insertRow(row); 123 } 124 } 125 126 public void addRow(Map<String,String> row, int index){ 127 rows.add(index, row); 128 if (columns.size() > 0) { 129 insertRow(row, index); 130 } 131 } 132 133 public void removeRow(int index){ 134 rows.remove(index); 135 bodyRow.removeViewAt(index); 136 } 137 138 public void removeRow(Map<String,String> row){ 139 int index = rows.indexOf(row); 140 this.removeRow(index); 141 } 142 143 public void setCellClickListener(CellClickListener cellClickListener) { 144 this.cellClickListener = cellClickListener; 145 } 146 147 public void setRowClickListener(RowClickListener rowClickListener) { 148 this.rowClickListener = rowClickListener; 149 } 150 151 public void setRowValidatorListener(RowValidatorListener rowValidatorListener) { 152 this.rowValidatorListener = rowValidatorListener; 153 } 154 155 public boolean isHasHeader() { 156 return hasHeader; 157 } 158 159 public void setHasHeader(boolean hasHeader) { 160 this.hasHeader = hasHeader; 161 } 162 163 private void insertColumn(DataGridViewColumn column){ 164 this.insertColumn(column, -1); 165 } 166 167 private void insertColumn(DataGridViewColumn column, int index){ 168 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 169 TextView headerTextView = (TextView) inflater.inflate(R.layout.ctrl_data_grid_view_column, headerRow, false); 170 headerTextView.setText(column.headerText); 171 headerTextView.setLayoutParams(new LayoutParams(column.columnWidth, LayoutParams.WRAP_CONTENT, 1)); 172 if (index == -1){ 173 headerRow.addView(headerTextView); 174 }else { 175 headerRow.addView(headerTextView, index); 176 } 177 } 178 179 public DataGridViewColumn getColumn(int index){ 180 return columns.get(index); 181 } 182 183 private void insertRow(final Map<String, String> row){ 184 this.insertRow(row, -1); 185 } 186 187 private void insertRow(final Map<String,String> row, int index){ 188 LinearLayout dataRow = new LinearLayout(getContext()); 189 dataRow.setOrientation(LinearLayout.HORIZONTAL); 190 dataRow.setSelected(true); 191 dataRow.setClickable(true); 192 dataRow.setBackgroundResource(R.drawable.datagrid_row_border); 193 dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 194 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 195 for (final DataGridViewColumn column : columns){ 196 String cellText = row.get(column.dataField); 197 TextView rowFieldText = (TextView) inflater.inflate(R.layout.ctrl_data_grid_view_cell, dataRow, false); 198 rowFieldText.setText(cellText); 199 rowFieldText.setGravity(column.textAlign); 200 rowFieldText.setTextColor(column.textColor); 201 rowFieldText.setLayoutParams(new LayoutParams(column.columnWidth, LayoutParams.WRAP_CONTENT, 1)); 202 dataRow.addView(rowFieldText); 203 if (column.isEnabled) { 204 rowFieldText.setOnClickListener(new OnClickListener() { 205 @Override 206 public void onClick(View v) { 207 if (cellClickListener != null) { 208 cellClickListener.onClick(row, column.dataField); 209 } 210 } 211 }); 212 } else { 213 rowFieldText.setTextColor(Color.rgb(128, 128, 128)); 214 } 215 } 216 if (rowValidatorListener != null){ 217 rowValidatorListener.onValidator(dataRow, row); 218 } 219 if (index == -1){ 220 bodyRow.addView(dataRow); 221 }else { 222 bodyRow.addView(dataRow, index); 223 } 224 dataRow.setOnClickListener(new View.OnClickListener() { 225 @Override 226 public void onClick(View v) { 227 if (rowClickListener != null) { 228 rowClickListener.onClick(row); 229 } 230 } 231 }); 232 } 233 234 public void updateRow(Map<String,String> row){ 235 int index = rows.indexOf(row); 236 bodyRow.removeViewAt(index); 237 this.insertRow(row, index); 238 } 239 240 public Map<String,String> getRow(int index) { 241 return rows.get(index); 242 } 243 244 public int getColumnsCount() { 245 return columns.size(); 246 } 247 248 public int getRowsCount() { 249 return rows.size(); 250 } 251 252 public interface CellClickListener{ 253 void onClick(Map<String,String> rowData, String dataField); 254 } 255 256 public interface RowClickListener{ 257 void onClick(Map<String,String> rowData); 258 } 259 260 public interface RowValidatorListener{ 261 void onValidator(ViewGroup v,Map<String,String> rowData); 262 } 263 }
代码里面用到的列属性类也附上:
1 public class DataGridViewColumn { 2 public String dataField; 3 public String headerText; 4 public int columnWidth; 5 public int textAlign; 6 public int textColor; 7 public boolean isEnabled; 8 }