热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android自定义DataGridView数据表格控件

这篇文章主要介绍了Android自定义DataGridView数据表格控件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

我是一个.net程序员,但是苦于公司要求开发一个android app,没办法,只能硬着头皮上了。

由于项目里面很多地方需要用到数据显示控件(类似于.net的DataGridView),度娘找了下发现没人公开类似的控件,没办法只好自己写了。

废话不多说,直接贴代码:

public class DataGridView extends HorizontalScrollView {
 private List columns;
 private List> 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();
  this.rows = new ArrayList>();
   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 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 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 row : rows){
      insertRow(row);
     }
    }
    return;
   }
  }
 }

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

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

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

 public void addRow(Map 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 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 row){
  this.insertRow(row, -1);
 }

 private void insertRow(final Map 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 row){
  int index = rows.indexOf(row);
  bodyRow.removeViewAt(index);
  this.insertRow(row, index);
 }

 public Map 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 rowData, String dataField);
 }

 public interface RowClickListener{
  void onClick(Map rowData);
 }

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

代码里面用到的列属性类也附上:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 本文介绍了在Mac上安装Xamarin并使用Windows上的VS开发iOS app的方法,包括所需的安装环境和软件,以及使用Xamarin.iOS进行开发的步骤。通过这种方法,即使没有Mac或者安装苹果系统,程序员们也能轻松开发iOS app。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • java代码利用aspose,java初学者代码
    本文目录一览:1、aspose.cellsjava合并excel ... [详细]
  • exportdefaultclassIndexextendsComponent{Taro.setNavigationBarTitle({title:this.$rout ... [详细]
  • 云开发与
    大家好,今天我来为大家分享一下,Linux命令查询小程序中的WePY云开发实践。WhyWePY首先,先分享一下为什么要选择WePY?在项目开始进行选型的时候,我可选的底层框架有We ... [详细]
  • C#datatable序列化后整数带有小数点或者小数点变成整数原来datatable的列有个datatype属性,可以指定为int类型或者decimal类型的,如果指定int类型, ... [详细]
author-avatar
兔牙的猪蹄
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有