本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

上次介绍了使用GridView实现表格,这次就说说如何用ListView实现自适应的表格。GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不一)。另外,GridView实现的表格可以定位在具体某个格单元,而ListView实现的表格则只能定位在表格行。因此还是那句老话:根据具体的使用环境而选择GridView 或者 ListView实现表格。

先贴出本文程序运行的效果图:

本文实现的ListView表格,可以每个格单元大小不一,文本(TextView)或图片(ImageView)做格单元的数据,不需要预先定义XML实现样式(自适应的根本目标)。由于ListView置于HorizontalScrollView中,因此对于列比较多/列数据比较长的数据表也能很好地适应其宽度。

main.xml源码如下:

View Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">  <HorizontalScrollView android:id="@+id/HorizontalScrollView01"  android:layout_height="fill_parent" android:layout_width="fill_parent">  <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"  android:layout_width="wrap_content"></ListView>  </HorizontalScrollView>
</LinearLayout>

主类testMyListView.java的源码如下:

View Code

package com.testMyListView;
import java.util.ArrayList;
import com.testMyListView.TableAdapter.TableCell;
import com.testMyListView.TableAdapter.TableRow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
/** * @author hellogv */
public class testMyListView extends Activity {  /** Called when the activity is first created. */  ListView lv;  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  this.setTitle("ListView自适应实现表格---hellogv");  lv = (ListView) this.findViewById(R.id.ListView01);  ArrayList<TableRow> table = new ArrayList<TableRow>();  TableCell[] titles = new TableCell[5];// 每行5个单元  int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;  // 定义标题  for (int i = 0; i < titles.length; i++) {  titles[i] = new TableCell("标题" + String.valueOf(i),   width + 8 * i,  LayoutParams.FILL_PARENT,   TableCell.STRING);  }  table.add(new TableRow(titles));  // 每行的数据  TableCell[] cells = new TableCell[5];// 每行5个单元  for (int i = 0; i < cells.length - 1; i++) {  cells[i] = new TableCell("No." + String.valueOf(i),  titles[i].width,   LayoutParams.FILL_PARENT,   TableCell.STRING);  }  cells[cells.length - 1] = new TableCell(R.drawable.icon,  titles[cells.length - 1].width,   LayoutParams.WRAP_CONTENT,  TableCell.IMAGE);  // 把表格的行添加到表格  for (int i = 0; i < 12; i++)  table.add(new TableRow(cells));  TableAdapter tableAdapter = new TableAdapter(this, table);  lv.setAdapter(tableAdapter);  lv.setOnItemClickListener(new ItemClickEvent());  }  class ItemClickEvent implements AdapterView.OnItemClickListener {  @Override  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  long arg3) {  Toast.makeText(testMyListView.this, "选中第"+String.valueOf(arg2)+"行", 500).show();  }  }
} 

ListView自适应实现Table的类TableAdapter.java代码如下:

PS:TableCell是格单元的类,TableRow是表格行的类,TableRowView是实现表格行的组件。实现步骤:TableCell --> TableRow(TableRowView)-->ListView

View Code

package com.testMyListView;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TableAdapter extends BaseAdapter {  private Context context;  private List<TableRow> table;  public TableAdapter(Context context, List<TableRow> table) {  this.context = context;  this.table = table;  }  @Override  public int getCount() {  return table.size();  }  @Override  public long getItemId(int position) {  return position;  }  public TableRow getItem(int position) {  return table.get(position);  }  public View getView(int position, View convertView, ViewGroup parent) {  TableRow tableRow = table.get(position);  return new TableRowView(this.context, tableRow);  }  /** * TableRowView 实现表格行的样式 * @author hellogv */  class TableRowView extends LinearLayout {  public TableRowView(Context context, TableRow tableRow) {  super(context);  this.setOrientation(LinearLayout.HORIZONTAL);  for (int i = 0; i < tableRow.getSize(); i++) {//逐个格单元添加到行  TableCell tableCell = tableRow.getCellValue(i);  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  tableCell.width, tableCell.height);//按照格单元指定的大小设置空间  layoutParams.setMargins(0, 0, 1, 1);//预留空隙制造边框  if (tableCell.type == TableCell.STRING) {//如果格单元是文本内容  TextView textCell = new TextView(context);  textCell.setLines(1);  textCell.setGravity(Gravity.CENTER);  textCell.setBackgroundColor(Color.BLACK);//背景黑色
                    textCell.setText(String.valueOf(tableCell.value));  addView(textCell, layoutParams);  } else if (tableCell.type == TableCell.IMAGE) {//如果格单元是图像内容  ImageView imgCell = new ImageView(context);  imgCell.setBackgroundColor(Color.BLACK);//背景黑色
                    imgCell.setImageResource((Integer) tableCell.value);  addView(imgCell, layoutParams);  }  }  this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙来实现边框
        }  }  /** * TableRow 实现表格的行 * @author hellogv */  static public class TableRow {  private TableCell[] cell;  public TableRow(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];  }  }  /** * TableCell 实现表格的格单元 * @author hellogv */  static public class TableCell {  static public final int STRING = 0;  static public final int IMAGE = 1;  public Object value;  public int width;  public int height;  private int type;  public TableCell(Object value, int width, int height, int type) {  this.value = value;  this.width = width;  this.height = height;  this.type = type;  }  }
}  

转载于:https://www.cnblogs.com/ok-lanyan/archive/2011/12/01/2271102.html

ListView自适应实现表格相关推荐

  1. Android 对Layout_weight属性完全解析以及使用ListView来实现表格

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/13630837 今天主要说的是对Layout_weight属性的完全解析,以及利用Lay ...

  2. ListView制作简单表格

    1.ArrayAdapter 适配器的作用是数据和视图之间的转换 ArrayList<E> data=new ArrayList<E>(); new ArrayAdapter& ...

  3. Vue 自适应高度表格的实现方法

    这篇文章主要介绍了Vue 自适应高度表格的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 前言 示例版本为 Elemen ...

  4. 自适应高度表格_【干货分享】Word制作表格,这几个技巧必须要学会!

    快点这里订阅最有趣有料的3D/VR/AR/Ai数字化资讯 如果你对Word不是很熟练,那制作表格起来可能特别费劲.这里,就给大家分享几个,必须要掌握的制表技巧. 01表格前面加空行 制作好表格以后,如 ...

  5. android listview 自适应列宽_Android 开发技术周报 Issue#272

    新闻/News 谷歌Pixel 4a将采用UFS 2.1存储:可以体验全套GMS [图]Android端Play商城现全面开放深色主题 教程/Tutorial OkHttp Interceptor - ...

  6. html自动适应布局,用纯CSS实现自适应布局表格

    以手机.平板等移动设备为平台的浏览行为变得越来越平常,甚至有些人叫嚣PC将死.虽然说的有些夸张,但让你的网站布局能够兼容PC和移动设备这些需求变得越来越重要.这种网页布局就是"自适应布局&q ...

  7. html表格整体放大,纯CSS实现自适应布局表格

    插件描述:如何让table元素也能表现出自适应性?按HTML5的说法,table是一个不鼓励使用的HTML标记,但现实工作中,我们避免不了的偶尔会用到它.那么,怎样让一个传统的表格也表现出自适应性呢? ...

  8. html图片自适应表格,如何用纯CSS实现自适应布局表格

    您可能感兴趣的话题: CSS 核心提示:这是自适应布局技术中的一个难题,如何让table元素也能表现出自适应性. 以手机.平板等移动设备为平台的浏览行为变得越来越平常,甚至有些人叫嚣PC将死.虽然说的 ...

  9. CSS_自适应_表格表单

    1.伪元素选择器 追加进来的内容属于行内元素 可以理解成:假的元素选择器===给他创造一个元素/标签 1) 匹配第一行的伪元素选择器 :first-line  这里的冒号可以使用::  : 双冒号和单 ...

最新文章

  1. 麻省理工学院韩松教授实验室招收科研实习生(可远程)
  2. C语言用DFS实现找到图的所有路径(邻接矩阵实现)
  3. 求出歌手的得分python_哪位大侠帮我做做python的题目啊,做其中5个就好。跪求啊...
  4. es6 --- Promise封装读取文件操作
  5. 什么是AES算法?(整合版)
  6. CS229 7.1应用机器学习中的一些技巧
  7. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
  8. 使用Hexo搭建专属Blog
  9. CSS之实现二级菜单动态出现
  10. 微信第三方平台相关的转发
  11. (转载)二进制与三进制的妙用
  12. 从零配置webpack(react+less+typescript+mobx)
  13. c语言程序设计二级考试内容,计算机二级考试C语言程序设计考试大纲
  14. 第三方支付接口怎么测试
  15. 自定义View时,用到Paint Canvas的一些温故,PropertyAnimation中的ObjectAnimator(动画三,“大大姐”的旋转跳跃)...
  16. SAP BAPI_EXCHANGERATE_GETDETAIL 读取货币汇率
  17. 谷歌邮箱服务器验证失败,Gmail错误:SMTP服务器需要安全连接,或者客户端未经身份验证。服务器响应为...
  18. Ant UI 的表单校验
  19. css3实现好看的边框效果
  20. H5页面设置背景图,微信可浏览背景图

热门文章

  1. 外呼机器人起名_电销外呼机器人如此受欢迎,今天终于知道原因了
  2. python中的self怎么理解_python中的self理解
  3. ai训练 样本大小_防止过拟合(三):数据增强(增加训练样本)
  4. jq监听input type=file发生改变,即选择文件,并获取文件名称
  5. php删除二维数组的某一行某一列_php中怎么去除二维数组的某个字段?
  6. mysql 主从 binlog_mysql 主从 binlog
  7. 通过官方查看springCloud,springBoot版本对应关系
  8. unity中链接字符串和变量显示_unity3d根据字符串读取属性.
  9. 计算机基础与应用32页,《计算机基础与应用》2次作业及答案
  10. oracle倒导数的条件过滤参数,oracle exp导出加上过滤条件