`

Android提高第十五篇之ListView自适应实现表格

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

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



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

main.xml源码如下:
<?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的源码如下:
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
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;  
        }  
    }  
}  
  • 大小: 1.8 MB
分享到:
评论
6 楼 明心Xin 2012-05-15  
楼主可以发我一份源码么。。我的邮箱tomcat553@163.com 谢了啊~~
5 楼 河边月 2012-05-06  
我的邮箱是   hebianyue@126.com
4 楼 河边月 2012-05-06  
楼主你好能把你的源代码发给我吗
3 楼 www_JE 2011-09-05  
楼主您好,我在运行到这的时候报空指针:
lv.setAdapter(tableAdapter);
跟了一下,tableAdapter里有内容,求解。
2 楼 gundumw100 2011-08-04  
arthurcsh 写道
没有选中单元格的。。

有,用GridView
1 楼 arthurcsh 2011-08-04  
没有选中单元格的。。

相关推荐

    Android提高第十五篇之ListView自适应实现表格.doc

    首先,我们看到描述中提到,与GridView相比,ListView更灵活,因为它的单元格大小可以自定义,但这也意味着实现自适应表格会更加复杂。GridView由于每个单元格大小固定,适合呈现结构化的表格,而ListView更适合需要...

    ListView自适应实现表格

    本项目“ListView自适应实现表格”旨在教你如何利用ListView创建出类似表格的效果,使数据以行和列的形式展示。下面将详细介绍这个知识点及其相关实现。 1. **自定义Adapter**: - 在实现ListView的表格效果时,...

    android自定义dialog嵌套listview自适应屏幕

    在Android开发中,自定义Dialog并使其嵌套ListView以实现屏幕自适应是一项常见的需求。这涉及到Android UI设计、自定义视图以及ListView的适配器机制。以下将详细阐述这个主题的相关知识点。 首先,让我们理解...

    Android提高之ListView实现自适应表格的方法

    GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(主要由于格单元大小不一)。此外,GridView实现的...

    android ListView实现表格

    在实现表格时,我们可以创建一个自定义的ListView,每个item视图设计为表格的一行,这样整个ListView就能呈现出表格的布局。 1. **自定义Adapter** - 创建一个新的类继承自BaseAdapter,如`MyTableAdapter`。 - ...

    listview实现表格效果,带表格线

    标题“listview实现表格效果,带表格线”表明我们要讨论的是如何利用ListView来模拟表格布局,并添加分割线以清晰地展示每个单元格的边界。 首先,我们要理解ListView的基本结构。ListView由多个ListView项...

    android自定义dialog+listview自适应屏幕显示.zip

    本项目“android自定义dialog+listview自适应屏幕显示”主要关注这两个方面的实现,确保界面在不同屏幕尺寸和分辨率下都能良好展示。下面我们将深入探讨相关知识点。 首先,我们来讨论自定义Dialog。在Android原生...

    android 使用ListView来实现表格

    - 如果需要更复杂的表格功能,如单元格合并、行高自适应等,可能需要使用第三方库,如`android-tablelayout`或`android-gridlayout`,或者自定义更复杂的Adapter和View。 通过以上步骤,我们就能在Android应用中...

    Android中ListView实现表格效果

    本篇文章将详细讲解如何在Android中利用ListView实现表格效果。 首先,我们需要理解ListView的工作原理。ListView通过Adapter来绑定数据,Adapter是连接数据源和视图的桥梁。我们可以通过自定义Adapter来定制...

    android用ListView实现表格样式

    本篇将详细介绍如何在Android中利用ListView实现表格样式的界面。 首先,理解ListView的基本原理是至关重要的。ListView依赖于Adapter来提供数据,它会根据Adapter返回的数据项数量动态创建视图(View)。为了实现...

    listview列宽自动适应

    在这里,我们使用`MeasureString`方法来测量文本的宽度,这是实现列宽自适应的核心步骤。 - **`Font`对象**:`ListView`中的字体属性,用于确保测量时使用的字体与实际显示的字体一致,从而得到准确的宽度值。 -...

    使用ListView来实现表格

    在本项目中,“使用ListView来实现表格”这一主题,主要是通过自定义ListView的Adapter来模拟表格的布局和行为。 首先,我们需要了解ListView的基本工作原理。ListView通过Adapter获取数据源,并将其转化为可显示的...

    Android中使用ListView实现表格形式的部局

    本教程将深入讲解如何在Android中使用ListView来实现表格布局,以及如何进行自定义以满足可变长度的表格布局需求,并添加简单的行组件单击事件。 首先,要实现基本的表格布局,我们需要创建一个自定义的列表项布局...

    Android提高篇之listView点击button翻页功能实现源码

    Android提高篇之listView点击button翻页功能实现源码,最近的开发需要在手机上实现列表分页功能,可以设置每页显示的记录数,第一页和最后一页翻页按钮自动置灰。代码中包括RelativeLayout对视图(View)和按钮位置...

    Android之用PopupWindow实现弹出listview形式的菜单

    在Android开发中,`PopupWindow`是一个非常实用的组件,它可以用来实现各种形式的弹出窗口,如下拉菜单、提示框等。本教程将详细讲解如何使用`PopupWindow`来创建一个以`ListView`形式展示的菜单。首先,我们需要...

    android的listview嵌套listview,列表嵌套列表 android studio版本

    总结来说,实现Android的ListView嵌套ListView需要理解ListView的工作机制,创建并管理两个Adapter,以及在布局文件中正确地嵌套ListView。这虽然不是特别高深的技术,但却是Android开发中常见的需求,熟练掌握能...

    Android中使用ListView绘制自定义表格技巧分享

    本篇将介绍如何使用ListView来绘制自定义表格,并实现一些高级特性,如动态列数、单元格合并以及自定义键盘输入。 首先,实现动态列数的关键在于自定义ListView的适配器。在提供的代码中,`SiteDetailViewAdapter`...

    Android横向列表,横向listview实现

    总之,Android的横向ListView实际上是通过RecyclerView实现的,通过设置LinearLayoutManager的水平方向,配合自定义Adapter和手势检测,我们可以创建出功能强大的横向滚动列表。这种布局方式广泛应用于各种场景,如...

    Android使用popwindow弹窗显示listview表格

    通过以上步骤,我们就实现了在Android中使用`PopupWindow`弹出一个包含`ListView`表格的效果。实际项目中,你可能需要根据需求调整布局、数据源、适配器等细节,但基本思路就是这样。记得在你的博客或其他平台上分享...

Global site tag (gtag.js) - Google Analytics