Android提高之ListView實現(xiàn)自適應(yīng)表格的方法
前面有文章介紹了使用GridView實現(xiàn)表格的方法,本文就來說說如何用ListView實現(xiàn)自適應(yīng)的表格。GridView比ListView更容易實現(xiàn)自適應(yīng)的表格,但是GridView每個格單元的大小固定,而ListView實現(xiàn)的表格可以自定義每個格單元的大小,但因此實現(xiàn)自適應(yīng)表格也會復(fù)雜些(主要由于格單元大小不一)。此外,GridView實現(xiàn)的表格可以定位在具體某個格單元,而ListView實現(xiàn)的表格則只能定位在表格行。因此還是那句老話:根據(jù)具體的使用環(huán)境而選擇GridView 或者 ListView實現(xiàn)表格。
先來看看本文程序運行的效果圖,如下圖所示:
本文實現(xiàn)的ListView表格,可以每個格單元大小不一,文本(TextView)或圖片(ImageView)做格單元的數(shù)據(jù),不需要預(yù)先定義XML實現(xiàn)樣式(自適應(yīng)的根本目標)。由于ListView置于HorizontalScrollView中,因此對于列比較多/列數(shù)據(jù)比較長的數(shù)據(jù)表也能很好地適應(yīng)其寬度。
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自適應(yīng)實現(xiàn)表格---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)); // 每行的數(shù)據(jù) 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自適應(yīng)實現(xiàn)Table的類TableAdapter.java代碼如下:
此處需要注意:TableCell是格單元的類,TableRow是表格行的類,TableRowView是實現(xiàn)表格行的組件。實現(xiàn)步驟: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 實現(xiàn)表格行的樣式 * @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);//按照格單元指定的大小設(shè)置空間 layoutParams.setMargins(0, 0, 1, 1);//預(yù)留空隙制造邊框 if (tableCell.type == TableCell.STRING) {//如果格單元是文本內(nèi)容 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) {//如果格單元是圖像內(nèi)容 ImageView imgCell = new ImageView(context); imgCell.setBackgroundColor(Color.BLACK);//背景黑色 imgCell.setImageResource((Integer) tableCell.value); addView(imgCell, layoutParams); } } this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙來實現(xiàn)邊框 } } /** * TableRow 實現(xiàn)表格的行 * @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 實現(xiàn)表格的格單元 * @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; } } }
希望本文所述實例能夠?qū)Υ蠹疫M行Android項目開發(fā)有所幫助。
- Android使用GridView實現(xiàn)表格分割線效果
- Android自定義View實現(xiàn)課程表表格
- Android自定義View實現(xiàn)仿GitHub的提交活躍表格
- Android listView 繪制表格實例詳解
- Android自定義DataGridView數(shù)據(jù)表格控件
- Android應(yīng)用中通過Layout_weight屬性用ListView實現(xiàn)表格
- Android中使用ListView實現(xiàn)漂亮的表格效果
- Android中使用ListView繪制自定義表格技巧分享
- android表格效果之ListView隔行變色實現(xiàn)代碼
- Android自定義view繪制表格的方法
相關(guān)文章
Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)
這篇文章主要為大家詳細介紹了Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android使用CrashHandler來獲取應(yīng)用的crash信息的方法
本篇文章主要介紹了Android使用CrashHandler來獲取應(yīng)用的crash信息的方法,具有一定的參考價值,有興趣的可以了解一下2017-09-09Android自定義ViewGroup實現(xiàn)選擇面板
ViewGroup是上面提到的所有的父控件的父類;但ViewGroup是一個抽象類,它里面有一個抽象方法onLayout,這個方法的作用就是擺放它所有的子控件(安排位置),因為是抽象類,不能直接new對象,所以我們在布局文件中不能直接使用 ViewGroup2022-07-07Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析講解
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析,在使用?Retrofit發(fā)起網(wǎng)絡(luò)請求時,我們可以通過定義一個接口并使用Retrofit的注解來描述這個接口中的請求,Retrofit會自動生成一個實現(xiàn)該接口的代理對象2023-03-03Android設(shè)備間實現(xiàn)藍牙(Bluetooth)共享上網(wǎng)
這篇文章主要為大家詳細介紹了Android設(shè)備間實現(xiàn)藍牙(Bluetooth)共享上網(wǎng)的方法,主要以圖片的方式向大家展示藍牙共享上網(wǎng)2016-03-03