- 浏览: 133181 次
- 性别:
- 来自: 广州
-
文章分类
最新评论
-
newhxj:
03-21 10:56:35.850: E/Web Conso ...
Android简易Flash播放器[转] -
roiz:
谢谢 很好正需要这资料
精确监听AbsListView滚动至底部[转]
上次介绍了使用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; } } }
本文来自http://blog.csdn.net/hellogv/article/details/6075014
发表评论
-
android Theme使用总结
2012-12-12 19:22 1490今天对api中style下的theme整个摸了一遍。 ... -
android优化——adapter
2012-12-12 18:56 1505什么是Adapter,可以先看看我的上一篇文章,Andr ... -
listview样式设置——自定义背景、分隔[转]
2012-12-12 14:13 822在Android中,ListView是最常用的一个控件, ... -
draw9patch不失真背景
2012-12-12 00:23 13241.背景自适应且不失真问题的存在 制作自适应背 ... -
android布局之selector(背景选择器)[转]
2012-12-11 23:07 2852关于listview和button都 ... -
android布局之RelativeLayout属性
2012-12-11 23:06 1233android:layout_above ... -
Android ListView下拉刷新点击加载更多[转]
2012-12-03 09:04 1707这个ListView的下拉刷新算是不错了。网上找了很多个 ... -
BitmapFactory.Options详解[转]
2012-11-21 20:50 3316public Bitmap in ... -
Android简易Flash播放器[转]
2012-01-20 21:16 1942上一节,大体说了下在Android程序中嵌套Flash动 ... -
将flash嵌入你的程序中[转]
2012-01-20 21:12 1222无论如何,我们需要一个android2.2的平板电脑 ... -
Android实现ListView异步加载图片[转]
2011-11-30 11:17 729ListView异步加载图片是非常实用的方法,凡是是要通过网络 ... -
精确监听AbsListView滚动至底部[转]
2011-11-10 09:00 1445用户使用android客户端时,当Lis ... -
可动态布局的Android抽屉之完整篇[转]
2011-11-10 09:00 1109上次介绍了基础篇,讲解了自定义抽屉控件的基 ... -
可动态布局的Android抽屉之基础[转]
2011-11-10 08:59 1589以前曾经介绍过《Android提高第十九篇 ... -
Android提高第二十一篇之MediaPlayer播放网络视频
2011-11-10 08:59 2640上次讲解了MediaPlayer播放网络音 ... -
Android提高第二十篇之MediaPlayer播放网络音频[转]
2011-11-10 08:58 789以前曾经地介绍过MediaPlayer的基本用 ... -
Android提高第十九篇之"多方向"抽屉[转]
2011-11-09 13:35 1203在android上要实现类似Launch的 ... -
Android提高十八篇之自定义Menu(TabMenu)[转]
2011-11-09 13:35 901用过UCWEB-Android版的人都应 ... -
Android提高十七篇之多级树形菜单的实现[转]
2011-11-09 13:35 896在Android里要实现树形菜单,都是用 ... -
Android提高十六篇之使用NDK把彩图转换灰度图[转]
2011-11-09 13:34 1059在Android上使 ...
相关推荐
首先,我们看到描述中提到,与GridView相比,ListView更灵活,因为它的单元格大小可以自定义,但这也意味着实现自适应表格会更加复杂。GridView由于每个单元格大小固定,适合呈现结构化的表格,而ListView更适合需要...
本项目“ListView自适应实现表格”旨在教你如何利用ListView创建出类似表格的效果,使数据以行和列的形式展示。下面将详细介绍这个知识点及其相关实现。 1. **自定义Adapter**: - 在实现ListView的表格效果时,...
在Android开发中,自定义Dialog并使其嵌套ListView以实现屏幕自适应是一项常见的需求。这涉及到Android UI设计、自定义视图以及ListView的适配器机制。以下将详细阐述这个主题的相关知识点。 首先,让我们理解...
GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(主要由于格单元大小不一)。此外,GridView实现的...
在实现表格时,我们可以创建一个自定义的ListView,每个item视图设计为表格的一行,这样整个ListView就能呈现出表格的布局。 1. **自定义Adapter** - 创建一个新的类继承自BaseAdapter,如`MyTableAdapter`。 - ...
标题“listview实现表格效果,带表格线”表明我们要讨论的是如何利用ListView来模拟表格布局,并添加分割线以清晰地展示每个单元格的边界。 首先,我们要理解ListView的基本结构。ListView由多个ListView项...
本项目“android自定义dialog+listview自适应屏幕显示”主要关注这两个方面的实现,确保界面在不同屏幕尺寸和分辨率下都能良好展示。下面我们将深入探讨相关知识点。 首先,我们来讨论自定义Dialog。在Android原生...
- 如果需要更复杂的表格功能,如单元格合并、行高自适应等,可能需要使用第三方库,如`android-tablelayout`或`android-gridlayout`,或者自定义更复杂的Adapter和View。 通过以上步骤,我们就能在Android应用中...
本篇文章将详细讲解如何在Android中利用ListView实现表格效果。 首先,我们需要理解ListView的工作原理。ListView通过Adapter来绑定数据,Adapter是连接数据源和视图的桥梁。我们可以通过自定义Adapter来定制...
本篇将详细介绍如何在Android中利用ListView实现表格样式的界面。 首先,理解ListView的基本原理是至关重要的。ListView依赖于Adapter来提供数据,它会根据Adapter返回的数据项数量动态创建视图(View)。为了实现...
在本项目中,“使用ListView来实现表格”这一主题,主要是通过自定义ListView的Adapter来模拟表格的布局和行为。 首先,我们需要了解ListView的基本工作原理。ListView通过Adapter获取数据源,并将其转化为可显示的...
在这里,我们使用`MeasureString`方法来测量文本的宽度,这是实现列宽自适应的核心步骤。 - **`Font`对象**:`ListView`中的字体属性,用于确保测量时使用的字体与实际显示的字体一致,从而得到准确的宽度值。 -...
本教程将深入讲解如何在Android中使用ListView来实现表格布局,以及如何进行自定义以满足可变长度的表格布局需求,并添加简单的行组件单击事件。 首先,要实现基本的表格布局,我们需要创建一个自定义的列表项布局...
Android提高篇之listView点击button翻页功能实现源码,最近的开发需要在手机上实现列表分页功能,可以设置每页显示的记录数,第一页和最后一页翻页按钮自动置灰。代码中包括RelativeLayout对视图(View)和按钮位置...
在Android开发中,`PopupWindow`是一个非常实用的组件,它可以用来实现各种形式的弹出窗口,如下拉菜单、提示框等。本教程将详细讲解如何使用`PopupWindow`来创建一个以`ListView`形式展示的菜单。首先,我们需要...
总结来说,实现Android的ListView嵌套ListView需要理解ListView的工作机制,创建并管理两个Adapter,以及在布局文件中正确地嵌套ListView。这虽然不是特别高深的技术,但却是Android开发中常见的需求,熟练掌握能...
本篇将介绍如何使用ListView来绘制自定义表格,并实现一些高级特性,如动态列数、单元格合并以及自定义键盘输入。 首先,实现动态列数的关键在于自定义ListView的适配器。在提供的代码中,`SiteDetailViewAdapter`...
总之,Android的横向ListView实际上是通过RecyclerView实现的,通过设置LinearLayoutManager的水平方向,配合自定义Adapter和手势检测,我们可以创建出功能强大的横向滚动列表。这种布局方式广泛应用于各种场景,如...
通过以上步骤,我们就实现了在Android中使用`PopupWindow`弹出一个包含`ListView`表格的效果。实际项目中,你可能需要根据需求调整布局、数据源、适配器等细节,但基本思路就是这样。记得在你的博客或其他平台上分享...