欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義DataGridView數(shù)據(jù)表格控件

 更新時間:2016年11月21日 08:54:20   作者:丿風(fēng)輕灬云淡  
這篇文章主要介紹了Android自定義DataGridView數(shù)據(jù)表格控件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

我是一個.net程序員,但是苦于公司要求開發(fā)一個android app,沒辦法,只能硬著頭皮上了。

由于項目里面很多地方需要用到數(shù)據(jù)顯示控件(類似于.net的DataGridView),度娘找了下發(fā)現(xiàn)沒人公開類似的控件,沒辦法只好自己寫了。

廢話不多說,直接貼代碼:

public class DataGridView extends HorizontalScrollView {
 private List<DataGridViewColumn> columns;
 private List<Map<String,String>> rows;

 private boolean hasHeader;

 private CellClickListener cellClickListener;
 private RowClickListener rowClickListener;
 private RowValidatorListener rowValidatorListener;
 private LinearLayout headerRow;
 private LinearLayout bodyRow;

 public DataGridView(Context context, AttributeSet attrs) {
  super(context, attrs);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DataGridView);
  hasHeader = a.getBoolean(R.styleable.DataGridView_hasHeader, true);
  a.recycle();

  LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  LinearLayout container = (LinearLayout) inflater.inflate(R.layout.ctrl_data_grid_view, null, false);
  addView(container);
  this.columns = new ArrayList<DataGridViewColumn>();
  this.rows = new ArrayList<Map<String,String>>();
   headerRow = new LinearLayout(getContext());
   headerRow.setOrientation(LinearLayout.HORIZONTAL);
   headerRow.setBackgroundResource(R.drawable.datagrid_header_background);
   headerRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  if (hasHeader){
   container.addView(headerRow);
  }
  ScrollView scrollView = (ScrollView)inflater.inflate(R.layout.ctrl_data_grid_view_scroll, container, false);
  bodyRow = (LinearLayout) inflater.inflate(R.layout.ctrl_data_grid_view, scrollView, false);
  scrollView.addView(bodyRow);
  container.addView(scrollView);
 }

 public void addColumn(String dataField, String headerText){
  this.addColumn(dataField, headerText, 200);
 }

 public void addColumn(String dataField, String headerText, int columnWidth){
  this.addColumn(dataField, headerText, columnWidth, Gravity.START);
 }

 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign){
  this.addColumn(dataField, headerText, columnWidth, textAlign, Color.rgb(80, 80, 80));
 }

 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign, int textColor){
  this.addColumn(dataField, headerText, columnWidth, textAlign, textColor, true);
 }

 public void addColumn(String dataField, String headerText, int columnWidth, int textAlign, int textColor, boolean isEnabled){
  DataGridViewColumn column = new DataGridViewColumn();
  column.dataField =dataField;
  column.headerText = headerText;
  column.columnWidth = columnWidth;
  column.textAlign = textAlign;
  column.textColor = textColor;
  column.isEnabled = isEnabled;
  this.addColumn(column);
 }

 public void addColumn(DataGridViewColumn column){
  columns.add(column);
  insertColumn(column);
  if (rows.size() > 0){
   bodyRow.removeAllViews();
   for (Map<String,String> row : rows){
    insertRow(row);
   }
  }
 }

 public void addColumn(DataGridViewColumn column, int index){
  columns.add(column);
  insertColumn(column, index);
  if (rows.size() > 0){
   bodyRow.removeAllViews();
   for (Map<String,String> row : rows){
    insertRow(row);
   }
  }
 }

 public void removeColumn(int index){
  columns.remove(index);
 }

 public void removeColumn(String dataField){
  for (DataGridViewColumn column : columns){
   if (column.dataField.equals(dataField)){
    this.removeColumn(column);
    if (rows.size() > 0){
     bodyRow.removeAllViews();
     for (Map<String,String> row : rows){
      insertRow(row);
     }
    }
    return;
   }
  }
 }

 public void removeColumn(DataGridViewColumn column){
  columns.remove(column);
 }

 public void setDataSource(List<Map<String,String>> rows){
  this.rows = rows;
  if (columns.size() > 0){
   bodyRow.removeAllViews();
   for (Map<String,String> row : rows){
    insertRow(row);
   }
  }
 }

 public void addRow(Map<String,String> row){
  rows.add(row);
  if (columns.size() > 0) {
   insertRow(row);
  }
 }

 public void addRow(Map<String,String> row, int index){
  rows.add(index, row);
  if (columns.size() > 0) {
   insertRow(row, index);
  }
 }

 public void removeRow(int index){
  rows.remove(index);
  bodyRow.removeViewAt(index);
 }

 public void removeRow(Map<String,String> row){
  int index = rows.indexOf(row);
  this.removeRow(index);
 }

 public void setCellClickListener(CellClickListener cellClickListener) {
  this.cellClickListener = cellClickListener;
 }

 public void setRowClickListener(RowClickListener rowClickListener) {
  this.rowClickListener = rowClickListener;
 }

 public void setRowValidatorListener(RowValidatorListener rowValidatorListener) {
  this.rowValidatorListener = rowValidatorListener;
 }

 public boolean isHasHeader() {
  return hasHeader;
 }

 public void setHasHeader(boolean hasHeader) {
  this.hasHeader = hasHeader;
 }

 private void insertColumn(DataGridViewColumn column){
  this.insertColumn(column, -1);
 }

 private void insertColumn(DataGridViewColumn column, int index){
  LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  TextView headerTextView = (TextView) inflater.inflate(R.layout.ctrl_data_grid_view_column, headerRow, false);
  headerTextView.setText(column.headerText);
  headerTextView.setLayoutParams(new LayoutParams(column.columnWidth, LayoutParams.WRAP_CONTENT, 1));
  if (index == -1){
   headerRow.addView(headerTextView);
  }else {
   headerRow.addView(headerTextView, index);
  }
 }

 public DataGridViewColumn getColumn(int index){
  return columns.get(index);
 }

 private void insertRow(final Map<String, String> row){
  this.insertRow(row, -1);
 }

 private void insertRow(final Map<String,String> row, int index){
  LinearLayout dataRow = new LinearLayout(getContext());
  dataRow.setOrientation(LinearLayout.HORIZONTAL);
  dataRow.setSelected(true);
  dataRow.setClickable(true);
  dataRow.setBackgroundResource(R.drawable.datagrid_row_border);
  dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  for (final DataGridViewColumn column : columns){
   String cellText = row.get(column.dataField);
   TextView rowFieldText = (TextView) inflater.inflate(R.layout.ctrl_data_grid_view_cell, dataRow, false);
   rowFieldText.setText(cellText);
   rowFieldText.setGravity(column.textAlign);
   rowFieldText.setTextColor(column.textColor);
   rowFieldText.setLayoutParams(new LayoutParams(column.columnWidth, LayoutParams.WRAP_CONTENT, 1));
   dataRow.addView(rowFieldText);
   if (column.isEnabled) {
    rowFieldText.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      if (cellClickListener != null) {
       cellClickListener.onClick(row, column.dataField);
      }
     }
    });
   } else {
    rowFieldText.setTextColor(Color.rgb(128, 128, 128));
   }
  }
  if (rowValidatorListener != null){
   rowValidatorListener.onValidator(dataRow, row);
  }
  if (index == -1){
   bodyRow.addView(dataRow);
  }else {
   bodyRow.addView(dataRow, index);
  }
  dataRow.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if (rowClickListener != null) {
     rowClickListener.onClick(row);
    }
   }
  });
 }

 public void updateRow(Map<String,String> row){
  int index = rows.indexOf(row);
  bodyRow.removeViewAt(index);
  this.insertRow(row, index);
 }

 public Map<String,String> getRow(int index) {
  return rows.get(index);
 }

 public int getColumnsCount() {
  return columns.size();
 }

 public int getRowsCount() {
  return rows.size();
 }

 public interface CellClickListener{
  void onClick(Map<String,String> rowData, String dataField);
 }

 public interface RowClickListener{
  void onClick(Map<String,String> rowData);
 }

 public interface RowValidatorListener{
  void onValidator(ViewGroup v,Map<String,String> rowData);
 }
}

代碼里面用到的列屬性類也附上:

public class DataGridViewColumn {
 public String dataField;
 public String headerText;
 public int columnWidth;
 public int textAlign;
 public int textColor;
 public boolean isEnabled;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ViewPager實現(xiàn)帶引導(dǎo)小圓點與自動跳轉(zhuǎn)的引導(dǎo)界面

    ViewPager實現(xiàn)帶引導(dǎo)小圓點與自動跳轉(zhuǎn)的引導(dǎo)界面

    這篇文章主要為大家詳細(xì)介紹了ViewPager實現(xiàn)帶引導(dǎo)小圓點與自動跳轉(zhuǎn)的引導(dǎo)界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android開發(fā)實現(xiàn)日期時間控件選擇

    Android開發(fā)實現(xiàn)日期時間控件選擇

    這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實現(xiàn)日期時間控件選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android?狀態(tài)管理之Lifecycle淺析

    Android?狀態(tài)管理之Lifecycle淺析

    這篇文章主要介紹了Android?狀態(tài)管理之Lifecycle淺析,Lifecycle主要用于Activity、Fragment這一類具有狀態(tài)的組件的狀態(tài)監(jiān)聽,更多相關(guān)資料介紹需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • RecycleView實現(xiàn)各種尺寸圖片展示

    RecycleView實現(xiàn)各種尺寸圖片展示

    這篇文章主要為大家詳細(xì)介紹了RecycleView實現(xiàn)各種尺寸圖片展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Kotlin超簡單實現(xiàn)StepView的方法

    Kotlin超簡單實現(xiàn)StepView的方法

    這篇文章主要介紹了Kotlin超簡單實現(xiàn)StepView的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 解決Android Studio電腦不支持HAXM的問題

    解決Android Studio電腦不支持HAXM的問題

    這篇文章主要介紹了Android Studio電腦不支持HAXM的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-03-03
  • Android 打開相冊選擇單張圖片實現(xiàn)代碼

    Android 打開相冊選擇單張圖片實現(xiàn)代碼

    這篇文章主要介紹了Android 打開相冊選擇單張圖片實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android 調(diào)用百度地圖API示例

    Android 調(diào)用百度地圖API示例

    在Android開發(fā)中有一個非常重要的應(yīng)用就是實時定位,通過手機在手機地圖上進(jìn)行實時定位,定位當(dāng)前手機的位置,這篇文章主要介紹了Android 調(diào)用百度地圖API示例,有興趣的可以了解一下。
    2017-01-01
  • Android基于ViewFilpper實現(xiàn)文字LED顯示效果示例

    Android基于ViewFilpper實現(xiàn)文字LED顯示效果示例

    這篇文章主要介紹了Android基于ViewFilpper實現(xiàn)文字LED顯示效果,結(jié)合完整實例形式分析了Android使用ViewFilpper實現(xiàn)文字LED顯示動畫效果的相關(guān)步驟與實現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • Android 開發(fā)系統(tǒng)自帶語音模塊應(yīng)用

    Android 開發(fā)系統(tǒng)自帶語音模塊應(yīng)用

    本篇文章 主要介紹 Android 開發(fā)自帶語音模塊實例,在開發(fā)Android系統(tǒng)中會用到系統(tǒng)語音搜索模塊,這里給大家一個參考實例
    2016-07-07

最新評論