欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Android提高第十五篇之ListView自適應(yīng)實現(xiàn)表格

系統(tǒng) 1905 0
上次介紹了 使用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)的根本目標(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)實現(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;  
        // 定義標(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個單元  
        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代碼如下:

PS: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;  
        }  
    }  
}  

  

Android提高第十五篇之ListView自適應(yīng)實現(xiàn)表格


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 五月婷六月丁香狠狠躁狠狠爱 | 欧美成人午夜剧场 | 国产精品毛片久久久久久久 | 九二淫黄大片看片 | 国产一区二区三区不卡在线观看 | 高清一区二区亚洲欧美日韩 | 99精品视频在线这里只有 | 性aaa| 啊啊啊好紧好爽 | 婷婷色激情 | 深夜你懂的在线网址入口 | 99久久精品免费看国产一区二区 | 天堂2014 | 亚洲欧美中文在线观看4 | 三人弄娇妻高潮3p视频 | 欧美人人干 | 久在线视频| 先锋影音av最新资源 | 99在线免费观看 | 国产精品一区视频 | 久草视频在线首页 | 日本在线播放不卡一区二区三区 | 免费国产小视频在线观看 | 三级av | 性欧美tube 精品 | 欧美一区二区三区不卡免费 | 日本又黄又粗暴的gif动态图含羞 | 岛国毛片一级一级特级毛片 | 在线播放三级 | 亚洲欧美在线视频免费 | 午夜免费观看福利片一区二区三区 | 成人欧美一区二区三区在线观看 | 色搞搞 | 99久久免费视频在线观看 | 久久亚洲在线 | 久久这里只有精品免费播放 | 亚洲午夜日韩高清一区 | 插入综合网 | 色图综合| 国产成人高清 | 国产成人理在线观看视频 |