惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(三十三)------之ListView自适应实现表格(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

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

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

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

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

main.xml源码如下:

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

主类testMyListView.java的源码如下:

  1. package com.testMyListView;  
  2. import java.util.ArrayList;  
  3. import com.testMyListView.TableAdapter.TableCell;  
  4. import com.testMyListView.TableAdapter.TableRow;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.ListView;  
  10. import android.widget.LinearLayout.LayoutParams;  
  11. import android.widget.Toast;  
  12.  
  13.  
  14.   
  15. public class testMyListView extends Activity {  
  16.       
  17.     ListView lv;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         this.setTitle("ListView自适应实现表格---hellogv");  
  23.         lv = (ListView) this.findViewById(R.id.ListView01);  
  24.         ArrayList<TableRow> table = new ArrayList<TableRow>();  
  25.         TableCell[] titles = new TableCell[5];  
  26.         int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;  
  27.           
  28.         for (int i = 0; i < titles.length; i++) {  
  29.             titles[i] = new TableCell("标题" + String.valueOf(i),   
  30.                                     width + 8 * i,  
  31.                                     LayoutParams.FILL_PARENT,   
  32.                                     TableCell.STRING);  
  33.         }  
  34.         table.add(new TableRow(titles));  
  35.           
  36.         TableCell[] cells = new TableCell[5];  
  37.         for (int i = 0; i < cells.length - 1; i++) {  
  38.             cells[i] = new TableCell("No." + String.valueOf(i),  
  39.                                     titles[i].width,   
  40.                                     LayoutParams.FILL_PARENT,   
  41.                                     TableCell.STRING);  
  42.         }  
  43.         cells[cells.length - 1] = new TableCell(R.drawable.icon,  
  44.                                                 titles[cells.length - 1].width,   
  45.                                                 LayoutParams.WRAP_CONTENT,  
  46.                                                 TableCell.IMAGE);  
  47.           
  48.         for (int i = 0; i < 12; i++)  
  49.             table.add(new TableRow(cells));  
  50.         TableAdapter tableAdapter = new TableAdapter(this, table);  
  51.         lv.setAdapter(tableAdapter);  
  52.         lv.setOnItemClickListener(new ItemClickEvent());  
  53.     }  
  54.     class ItemClickEvent implements AdapterView.OnItemClickListener {  
  55.         @Override  
  56.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  57.                 long arg3) {  
  58.             Toast.makeText(testMyListView.this"选中第"+String.valueOf(arg2)+"行"500).show();  
  59.         }  
  60.     }  
  61. }  

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

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

  1. package com.testMyListView;  
  2. import java.util.List;  
  3. import android.content.Context;  
  4. import android.graphics.Color;  
  5. import android.view.Gravity;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.ImageView;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.TextView;  
  12. public class TableAdapter extends BaseAdapter {  
  13.     private Context context;  
  14.     private List<TableRow> table;  
  15.     public TableAdapter(Context context, List<TableRow> table) {  
  16.         this.context = context;  
  17.         this.table = table;  
  18.     }  
  19.     @Override  
  20.     public int getCount() {  
  21.         return table.size();  
  22.     }  
  23.     @Override  
  24.     public long getItemId(int position) {  
  25.         return position;  
  26.     }  
  27.     public TableRow getItem(int position) {  
  28.         return table.get(position);  
  29.     }  
  30.     public View getView(int position, View convertView, ViewGroup parent) {  
  31.         TableRow tableRow = table.get(position);  
  32.         return new TableRowView(this.context, tableRow);  
  33.     }  
  34.      
  35.  
  36.  
  37.   
  38.     class TableRowView extends LinearLayout {  
  39.         public TableRowView(Context context, TableRow tableRow) {  
  40.             super(context);  
  41.               
  42.             this.setOrientation(LinearLayout.HORIZONTAL);  
  43.             for (int i = 0; i < tableRow.getSize(); i++) {  
  44.                 TableCell tableCell = tableRow.getCellValue(i);  
  45.                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  46.                         tableCell.width, tableCell.height);  
  47.                 layoutParams.setMargins(0011);  
  48.                 if (tableCell.type == TableCell.STRING) {  
  49.                     TextView textCell = new TextView(context);  
  50.                     textCell.setLines(1);  
  51.                     textCell.setGravity(Gravity.CENTER);  
  52.                     textCell.setBackgroundColor(Color.BLACK);  
  53.                     textCell.setText(String.valueOf(tableCell.value));  
  54.                     addView(textCell, layoutParams);  
  55.                 } else if (tableCell.type == TableCell.IMAGE) {  
  56.                     ImageView imgCell = new ImageView(context);  
  57.                     imgCell.setBackgroundColor(Color.BLACK);  
  58.                     imgCell.setImageResource((Integer) tableCell.value);  
  59.                     addView(imgCell, layoutParams);  
  60.                 }  
  61.             }  
  62.             this.setBackgroundColor(Color.WHITE);  
  63.         }  
  64.     }  
  65.      
  66.  
  67.  
  68.   
  69.     static public class TableRow {  
  70.         private TableCell[] cell;  
  71.         public TableRow(TableCell[] cell) {  
  72.             this.cell = cell;  
  73.         }  
  74.         public int getSize() {  
  75.             return cell.length;  
  76.         }  
  77.         public TableCell getCellValue(int index) {  
  78.             if (index >= cell.length)  
  79.                 return null;  
  80.             return cell[index];  
  81.         }  
  82.     }  
  83.      
  84.  
  85.  
  86.   
  87.     static public class TableCell {  
  88.         static public final int STRING = 0;  
  89.         static public final int IMAGE = 1;  
  90.         public Object value;  
  91.         public int width;  
  92.         public int height;  
  93.         private int type;  
  94.         public TableCell(Object value, int width, int height, int type) {  
  95.             this.value = value;  
  96.             this.width = width;  
  97.             this.height = height;  
  98.             this.type = type;  
  99.         }  
  100.     }  
  101. }