android 之 GridView

來源:互聯網
上載者:User

GridView 的用法基本與ListView類似。

程式布局檔案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">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <GridView android:id="@+id/gridview01" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

其中GridView每一行的布局檔案grid_row.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView android:id="@+id/imageview01" android:scaleType="fitXY"
        android:layout_width="50dip" android:layout_height="50dip" />
    <TextView android:id="@+id/tv01" android:layout_width="100dip"
        android:layout_height="wrap_content" android:textSize="24dip"
        android:paddingLeft="5dip" />
    <TextView android:id="@+id/tv02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="24dip"
        android:paddingLeft="5dip" />
</LinearLayout>

在主函數中配置GridView的Adapter:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   
    gridview = (GridView) findViewById(R.id.gridview01);
    SimpleAdapter adapter = new SimpleAdapter(this, generateDataList(),
            R.layout.grid_row, new String[] { "col1", "col2", "col3" },
            new int[] { R.id.imageview01, R.id.tv01, R.id.tv02 });
    gridview.setAdapter(adapter);
}

其中generateDataList()產生Adapter中的資料,其類型為 List<? extends Map<String, ?>>:

private List<? extends Map<String, ?>> generateDataList() {
        // TODO Auto-generated method stub
        ArrayList<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        int rowCount=drawableIds.length;
        for(int i=0;i<rowCount;i++){
            HashMap<String, Object> hmap=new HashMap<String, Object>();
            hmap.put("col1", drawableIds[i]);
            hmap.put("col2", this.getResources().getString(nameIds[i]));
            hmap.put("col3", this.getResources().getString(msgIds[i]));
            list.add(hmap);
        }
        return list;
    }

為GridView添加事件:

gridview.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                TextView textview = (TextView) findViewById(R.id.textview01);
                LinearLayout ll = (LinearLayout) arg1;
                TextView tv01 = (TextView) ll.getChildAt(1);
                TextView tv02 = (TextView) ll.getChildAt(2);
                StringBuilder sb = new StringBuilder();
                sb.append(tv01.getText());
                sb.append(" ");
                sb.append(tv02.getText());
                textview.setText(sb.toString());

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        gridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                LinearLayout ll = (LinearLayout) arg1;
                TextView tv01 = (TextView) ll.getChildAt(1);
                TextView tv02 = (TextView) ll.getChildAt(2);
                StringBuilder sb = new StringBuilder();
                sb.append(tv01.getText());
                sb.append(" ");
                sb.append(tv02.getText());
                Toast.makeText(mainActivity.this, sb.toString(),
                        Toast.LENGTH_LONG).show();
            }
        });

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.