Android提高第十五篇之ListView自适应实现表格
本文来自http://blog.****.net/hellogv/,引用必须注明出处!
上次介绍了使用GridView实现表格,这次就说说如何用ListView实现自适应的表格。GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不一)。另外,GridView实现的表格可以定位在具体某个格单元,而ListView实现的表格则只能定位在表格行。因此还是那句老话:根据具体的使用环境而选择GridView 或者 ListView实现表格。
先贴出本文程序运行的效果图:
本文实现的ListView表格,可以每个格单元大小不一,文本(TextView)或图片(ImageView)做格单元的数据,不需要预先定义XML实现样式(自适应的根本目标)。由于ListView置于HorizontalScrollView中,因此对于列比较多/列数据比较长的数据表也能很好地适应其宽度。
main.xml源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <HorizontalScrollViewandroid:id="@+id/HorizontalScrollView01"
- android:layout_height="fill_parent"android:layout_width="fill_parent">
- <ListViewandroid:id="@+id/ListView01"android:layout_height="wrap_content"
- android:layout_width="wrap_content"></ListView>
- </HorizontalScrollView>
- </LinearLayout>
主类testMyListView.java的源码如下:
- packagecom.testMyListView;
- importjava.util.ArrayList;
- importcom.testMyListView.TableAdapter.TableCell;
- importcom.testMyListView.TableAdapter.TableRow;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.ListView;
- importandroid.widget.LinearLayout.LayoutParams;
- importandroid.widget.Toast;
- /**
- *@authorhellogv
- */
- publicclasstestMyListViewextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- ListViewlv;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("ListView自适应实现表格---hellogv");
- lv=(ListView)this.findViewById(R.id.ListView01);
- ArrayList<TableRow>table=newArrayList<TableRow>();
- TableCell[]titles=newTableCell[5];//每行5个单元
- intwidth=this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;
- //定义标题
- for(inti=0;i<titles.length;i++){
- titles[i]=newTableCell("标题"+String.valueOf(i),
- width+8*i,
- LayoutParams.FILL_PARENT,
- TableCell.STRING);
- }
- table.add(newTableRow(titles));
- //每行的数据
- TableCell[]cells=newTableCell[5];//每行5个单元
- for(inti=0;i<cells.length-1;i++){
- cells[i]=newTableCell("No."+String.valueOf(i),
- titles[i].width,
- LayoutParams.FILL_PARENT,
- TableCell.STRING);
- }
- cells[cells.length-1]=newTableCell(R.drawable.icon,
- titles[cells.length-1].width,
- LayoutParams.WRAP_CONTENT,
- TableCell.IMAGE);
- //把表格的行添加到表格
- for(inti=0;i<12;i++)
- table.add(newTableRow(cells));
- TableAdaptertableAdapter=newTableAdapter(this,table);
- lv.setAdapter(tableAdapter);
- lv.setOnItemClickListener(newItemClickEvent());
- }
- classItemClickEventimplementsAdapterView.OnItemClickListener{
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- Toast.makeText(testMyListView.this,"选中第"+String.valueOf(arg2)+"行",500).show();
- }
- }
- }
ListView自适应实现Table的类TableAdapter.java代码如下:
PS:TableCell是格单元的类,TableRow是表格行的类,TableRowView是实现表格行的组件。实现步骤:TableCell --> TableRow(TableRowView)-->ListView
- packagecom.testMyListView;
- importjava.util.List;
- importandroid.content.Context;
- importandroid.graphics.Color;
- importandroid.view.Gravity;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importandroid.widget.TextView;
- publicclassTableAdapterextendsBaseAdapter{
- privateContextcontext;
- privateList<TableRow>table;
- publicTableAdapter(Contextcontext,List<TableRow>table){
- this.context=context;
- this.table=table;
- }
- @Override
- publicintgetCount(){
- returntable.size();
- }
- @Override
- publiclonggetItemId(intposition){
- returnposition;
- }
- publicTableRowgetItem(intposition){
- returntable.get(position);
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- TableRowtableRow=table.get(position);
- returnnewTableRowView(this.context,tableRow);
- }
- /**
- *TableRowView实现表格行的样式
- *@authorhellogv
- */
- classTableRowViewextendsLinearLayout{
- publicTableRowView(Contextcontext,TableRowtableRow){
- super(context);
- this.setOrientation(LinearLayout.HORIZONTAL);
- for(inti=0;i<tableRow.getSize();i++){//逐个格单元添加到行
- TableCelltableCell=tableRow.getCellValue(i);
- LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(
- tableCell.width,tableCell.height);//按照格单元指定的大小设置空间
- layoutParams.setMargins(0,0,1,1);//预留空隙制造边框
- if(tableCell.type==TableCell.STRING){//如果格单元是文本内容
- TextViewtextCell=newTextView(context);
- textCell.setLines(1);
- textCell.setGravity(Gravity.CENTER);
- textCell.setBackgroundColor(Color.BLACK);//背景黑色
- textCell.setText(String.valueOf(tableCell.value));
- addView(textCell,layoutParams);
- }elseif(tableCell.type==TableCell.IMAGE){//如果格单元是图像内容
- ImageViewimgCell=newImageView(context);
- imgCell.setBackgroundColor(Color.BLACK);//背景黑色
- imgCell.setImageResource((Integer)tableCell.value);
- addView(imgCell,layoutParams);
- }
- }
- this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙来实现边框
- }
- }
- /**
- *TableRow实现表格的行
- *@authorhellogv
- */
- staticpublicclassTableRow{
- privateTableCell[]cell;
- publicTableRow(TableCell[]cell){
- this.cell=cell;
- }
- publicintgetSize(){
- returncell.length;
- }
- publicTableCellgetCellValue(intindex){
- if(index>=cell.length)
- returnnull;
- returncell[index];
- }
- }
- /**
- *TableCell实现表格的格单元
- *@authorhellogv
- */
- staticpublicclassTableCell{
- staticpublicfinalintSTRING=0;
- staticpublicfinalintIMAGE=1;
- publicObjectvalue;
- publicintwidth;
- publicintheight;
- privateinttype;
- publicTableCell(Objectvalue,intwidth,intheight,inttype){
- this.value=value;
- this.width=width;
- this.height=height;
- this.type=type;
- }
- }
- }