<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      ListView自適應(yīng)實(shí)現(xiàn)表格

      本文來自http://blog.csdn.net/hellogv/ ,引用必須注明出處!

             上次介紹了使用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中,因此對(duì)于列比較多/列數(shù)據(jù)比較長(zhǎng)的數(shù)據(jù)表也能很好地適應(yīng)其寬度。

      main.xml源碼如下:

      View Code
      <?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的源碼如下:

      View Code
      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代碼如下:

      PS:TableCell是格單元的類,TableRow是表格行的類,TableRowView是實(shí)現(xiàn)表格行的組件。實(shí)現(xiàn)步驟:TableCell --> TableRow(TableRowView)-->ListView

      View Code
      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;  
              }  
          }  
      }  
      posted @ 2011-12-01 19:43  ok_lanyan  閱讀(3734)  評(píng)論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 日韩人妻系列无码专区| 国产白嫩护士在线播放| 久播影院无码中文字幕| 国产精品成人免费视频网站京东| 成人av专区精品无码国产| 国产精品无码成人午夜电影| 一个人看的www视频免费观看| 天干天干夜天干天天爽| 久久AV中文综合一区二区| 乱码精品一区二区三区| 亚洲成人动漫av在线| 亚洲色最新高清AV网站| 国产初高中生视频在线观看| 国产精品久久中文字幕| 亚洲中文字幕久久精品码| 亚洲高清日韩专区精品| 亚洲中文字幕无码一区无广告| 欧美牲交a欧美牲交aⅴ免费| 91密桃精品国产91久久| 国产精品久久无码不卡黑寡妇| 欧美激情精品久久| 久久精品国产99精品亚洲| 亚洲人妻系列中文字幕| 99RE6在线观看国产精品| 国产成年女人特黄特色大片免费| 欧美精品一产区二产区| 亚洲精品中文字幕码专区| 昌平区| 久久99国产精品尤物| 97色伦97色伦国产| 欧美日韩综合网| 最新av中文字幕无码专区| 国产精品久久久久久久久久| av午夜福利一片免费看久久| 国产91丝袜在线观看| 又爽又黄又无遮掩的免费视频| 日韩av片无码一区二区三区| 鲁大师在线视频播放免费观看| 18禁亚洲一区二区三区| 国产成人午夜福利院| 国色精品卡一卡2卡3卡4卡在线|