Android基础--GridView控件

来源:互联网 发布:博库数据 编辑:程序博客网 时间:2024/05/16 17:24

一)布局文件

main.xml

<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"  xmlns:android="http://schemas.android.com/apk/res/android">     <TextView       android:layout_height="wrap_content"       android:layout_width="fill_parent"        android:textSize="24dip"        android:textColor="@color/white"       android:text="@string/hello"        android:id="@+id/TextView01">   </TextView>     <GridView       android:layout_height="fill_parent"        android:layout_width="fill_parent"        android:id="@+id/GridView01"       android:stretchMode="columnWidth" android:horizontalSpacing="5dip"  android:verticalSpacing="5dip"> </GridView> </LinearLayout>

grid_row.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout   android:id="@+id/LinearLayout01"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:orientation="horizontal"  xmlns:android="http://schemas.android.com/apk/res/android">  <ImageView    android:id="@+id/ImageView01"    android:scaleType="fitXY"   android:layout_width="100dip"   android:layout_height="98dip"></ImageView><TextView     android:id="@+id/TextView02"     android:layout_width="140dip"     android:layout_height="wrap_content"    android:textColor="@color/white"        android:textSize="24dip"        android:paddingLeft="5dip">    </TextView></LinearLayout>

二)MyActivity.java

package com.howell.sample6_7;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.GridView;import android.widget.LinearLayout;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;public class MainActivity extends Activity {int[] drawableIds = {R.drawable.cc, R.drawable.gg, R.drawable.qbmy,R.drawable.lsdf, R.drawable.shg};int[] nameIds = {R.string.cc, R.string.gg, R.string.qbmy, R.string.lsdf, R.string.shg};public List<? extends Map<String, ?>> generateDataList(){ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();int rowCounter = drawableIds.length;for (int i = 0; i < rowCounter; i++){HashMap<String, Object> hmap = new HashMap<String, Object>();hmap.put("col1", drawableIds[i]);hmap.put("col2", this.getResources().getString(nameIds[i]));list.add(hmap);}return list;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);GridView gv = (GridView)this.findViewById(R.id.GridView01);SimpleAdapter sca = new SimpleAdapter(this, generateDataList(),R.layout.grid_row,new String[]{"col1", "col2"}, new int[]{R.id.ImageView01, R.id.TextView02});gv.setAdapter(sca);gv.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {// TODO Auto-generated method stubTextView tv = (TextView)findViewById(R.id.TextView01);LinearLayout ll = (LinearLayout)arg1;TextView tvn = (TextView)ll.getChildAt(1);StringBuilder sb = new StringBuilder();sb.append(tvn.getText());tv.setText(sb.toString());}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});gv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubTextView tv = (TextView)findViewById(R.id.TextView01);LinearLayout ll = (LinearLayout)arg1;TextView tvn = (TextView)ll.getChildAt(1);StringBuilder sb = new StringBuilder();sb.append(tvn.getText());tv.setText(sb.toString());}});}}