最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android提高之ListView實(shí)現(xiàn)自適應(yīng)表格的方法

 更新時(shí)間:2014年08月09日 11:14:41   投稿:shichen2014  
這篇文章主要介紹了Android采用ListView實(shí)現(xiàn)自適應(yīng)表格的方法,比較實(shí)用的功能,需要的朋友可以參考下

前面有文章介紹了使用GridView實(shí)現(xiàn)表格的方法,本文就來說說如何用ListView實(shí)現(xiàn)自適應(yīng)的表格。GridView比ListView更容易實(shí)現(xiàn)自適應(yīng)的表格,但是GridView每個(gè)格單元的大小固定,而ListView實(shí)現(xiàn)的表格可以自定義每個(gè)格單元的大小,但因此實(shí)現(xiàn)自適應(yīng)表格也會(huì)復(fù)雜些(主要由于格單元大小不一)。此外,GridView實(shí)現(xiàn)的表格可以定位在具體某個(gè)格單元,而ListView實(shí)現(xiàn)的表格則只能定位在表格行。因此還是那句老話:根據(jù)具體的使用環(huán)境而選擇GridView 或者 ListView實(shí)現(xiàn)表格。

先來看看本文程序運(yùn)行的效果圖,如下圖所示:

本文實(shí)現(xiàn)的ListView表格,可以每個(gè)格單元大小不一,文本(TextView)或圖片(ImageView)做格單元的數(shù)據(jù),不需要預(yù)先定義XML實(shí)現(xiàn)樣式(自適應(yīng)的根本目標(biāo))。由于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)實(shí)現(xiàn)表格---hellogv");
 lv = (ListView) this.findViewById(R.id.ListView01);
 ArrayList<TableRow> table = new ArrayList<TableRow>();
 TableCell[] titles = new TableCell[5];// 每行5個(gè)單元
 int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;
 // 定義標(biāo)題
 for (int i = 0; i < titles.length; i++) {
  titles[i] = new TableCell("標(biāo)題" + String.valueOf(i), 
   width + 8 * i,
   LayoutParams.FILL_PARENT, 
   TableCell.STRING);
 }
 table.add(new TableRow(titles));
 // 每行的數(shù)據(jù)
 TableCell[] cells = new TableCell[5];// 每行5個(gè)單元
 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)實(shí)現(xiàn)Table的類TableAdapter.java代碼如下:

此處需要注意:TableCell是格單元的類,TableRow是表格行的類,TableRowView是實(shí)現(xiàn)表格行的組件。實(shí)現(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 實(shí)現(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++) {//逐個(gè)格單元添加到行
  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);//背景白色,利用空隙來實(shí)現(xiàn)邊框
 }
 }
 /**
 * TableRow 實(shí)現(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 實(shí)現(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;
 }
 }
}

希望本文所述實(shí)例能夠?qū)Υ蠹疫M(jìn)行Android項(xiàng)目開發(fā)有所幫助。

相關(guān)文章

  • Android自定義View之繪制圓形頭像功能

    Android自定義View之繪制圓形頭像功能

    這篇文章主要介紹了Android自定義View之繪制圓形頭像功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Android studio六大基本布局詳解

    Android studio六大基本布局詳解

    這篇文章主要介紹了Android常用的布局方式:線性布局,相對布局,表格布局,層布局,絕對布局,網(wǎng)格布局,用的相對較多的是線性布局和相對布局。感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • Android組件banner實(shí)現(xiàn)左右滑屏效果

    Android組件banner實(shí)現(xiàn)左右滑屏效果

    這篇文章主要為大家詳細(xì)介紹了Android組件banner實(shí)現(xiàn)左右滑屏效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android實(shí)現(xiàn)水波紋外擴(kuò)效果的實(shí)例代碼

    Android實(shí)現(xiàn)水波紋外擴(kuò)效果的實(shí)例代碼

    微信曾經(jīng)推出了一個(gè)查找附近好友的功能,大致功能是這樣的:屏幕上有一個(gè)按鈕,長按按鈕的時(shí)候,會(huì)有一圈圈水波紋的動(dòng)畫向外擴(kuò)散,松手后,動(dòng)畫結(jié)束
    2018-05-05
  • Android自定義View實(shí)現(xiàn)圓形環(huán)繞效果

    Android自定義View實(shí)現(xiàn)圓形環(huán)繞效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形環(huán)繞效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android zxing如何識別反轉(zhuǎn)二維碼詳解

    Android zxing如何識別反轉(zhuǎn)二維碼詳解

    這篇文章主要給大家介紹了關(guān)于Android zxing如何識別反轉(zhuǎn)二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Android自定義popupwindow實(shí)例代碼

    Android自定義popupwindow實(shí)例代碼

    這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android簡單實(shí)現(xiàn)自定義彈框(PopupWindow)

    Android簡單實(shí)現(xiàn)自定義彈框(PopupWindow)

    本文主要介紹了Android利用PopupWindow實(shí)現(xiàn)自定義彈框的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • Android LinearLayout實(shí)現(xiàn)自動(dòng)換行

    Android LinearLayout實(shí)現(xiàn)自動(dòng)換行

    這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實(shí)現(xiàn)自動(dòng)換行,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android App安裝列表獲取方法(實(shí)踐方案)

    Android App安裝列表獲取方法(實(shí)踐方案)

    文章介紹了Android 11及以上版本獲取應(yīng)用列表的方案調(diào)整,包括權(quán)限配置、白名單配置和action配置三種方式,并提供了相應(yīng)的Java和Kotlin代碼示例,建議在Android 15及以上版本中使用action方式獲取應(yīng)用列表,感興趣的朋友一起看看吧
    2025-03-03

最新評論

郧西县| 曲阜市| 凌云县| 厦门市| 宁晋县| 东莞市| 赤城县| 茂名市| 金湖县| 安岳县| 武山县| 巴彦县| 郯城县| 大田县| 海林市| 军事| 泗洪县| 绍兴市| 沙雅县| 长阳| 太和县| 中西区| 铜川市| 毕节市| 霞浦县| 民勤县| 枞阳县| 渝北区| 三河市| 郁南县| 垣曲县| 永丰县| 隆昌县| 江阴市| 东阿县| 渝中区| 浦江县| 宜阳县| 阿克苏市| 彩票| 高雄市|