首页 > 其他分享 >Android提高第十五篇之ListView自适应实现表格

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

时间:2023-05-01 14:03:44浏览次数:52  
标签:TableCell 表格 int 第十五 import Android ListView public android


上次介绍了 使用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;  
        }  
    }  
}


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

  • 大小: 1.8 MB
  • 查看图片附件

标签:TableCell,表格,int,第十五,import,Android,ListView,public,android
From: https://blog.51cto.com/u_5454003/6238871

相关文章

  • Android提高第八篇之SQLite分页读取
    Android包含了常用于嵌入式系统的SQLite,免去了开发者自己移植安装的功夫。SQLite支持多数SQL92标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准的SQL语句,毕竟这样容易在......
  • Android提高第九篇之GridView和SQLite实现分页表格
    上次讲的Android上的SQLite分页读取,只用文本框显示数据而已,这次就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据。先来看看本文程序运行的动画:这个SQL分页表格控件主要分为“表格区”和“分页栏”这两部分,这两部分都是基于GridView实现的。......
  • Android提高第四篇之Activity+Intent
          Android有三个基础组件Activity,Service和BroadcastReceiver,他们都是依赖Intent来启动。本文介绍的是Activity的生命周期以及针对Activity的Intent使用。       之前的例子一直都是使用Activity,在一个LayoutXML与一个Activity捆绑的情况下可以视为一个Form,......
  • Android提高第十八篇之自定义PopupWindow实现的Menu(TabMenu)
    用过UCWEB-Android版的人都应该对其特殊的menu有印象,把menu做成Tab-Menu(支持分页的Menu),可以容纳比Android传统的menu更丰富的内容(Android的menu超过6项则缩略在[更多]里),本文参考网上的例子(作者:CoffeeCole,email:[email protected]),对例子进行简化以及封装,使其作为一个复......
  • Android Activity界面切换添加动画特效
    在Android2.0之后有了overridePendingTransition(),其中里面两个参数,一个是前一个activity的退出两一个activity的进入。@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentVi......
  • android系统各种音量的获取与设置 以及监听音量变化
    获取系统音量通过程序获取android系统手机的铃声和音量。同样,设置铃声和音量的方法也很简单!设置音量的方法也很简单,AudioManager提供了方法:publicvoidsetStreamVolume(intstreamType,intindex,intflags)其中streamType有内置的常量,去文档里面就可以看到。JAVA代码:AudioManagermAud......
  • android2.3新增API StrictMode介绍
    google在android2.3中新增了StrictModeAPI来设置对一个thread的策略(ui线程或者分线程),它主要检测了读写操作,访问网络,数据库读写等耗时的操作并将其以log或者dialog等形式打印出来。分析这些日志,我们可以尽快找出程序运行缓慢的原因进而优化代码,避免ANR(ApplicationNotRespondin......
  • 一个android的webview的例子
    截图如图所示。核心部分代码packagecom.example.app;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importandroid.support.v7.app.ActionBarActivity;importandroid.support.v7.app.ActionBar;importandroid.support.v4.app.Fragment;......
  • Android手机屏幕锁屏监测
    手机屏幕锁屏和解锁都是会发广播出来的,我们只要用BroadcaseReceiver来监听相应的Action即可,必须动态在代码中注册才能够接受到广播。1.publicvoidonCreate(finalBundlesavedInstanceState){2.finalIntentFilterfilter=newIntentFilter();3.filter......
  • Android常用工具类
    Android常用工具类很好很强大 http://www.trinea.cn/android/android-common-utils/ https://github.com/wyouflf/xUtils Android11个常用工具类:(见附件)......