android学习---gridview
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" android:orientation="horizontal" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:horizontalSpacing="10dp" android:numColumns="3" android:padding="20dp" android:verticalSpacing="10dp" > </GridView> <ImageView android:id="@+id/imageview" android:layout_width="fill_parent" android:layout_height="150dp" > </ImageView> </LinearLayout>
新建xml文件,cell.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="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="72dp" android:layout_height="72dp" > </ImageView> </LinearLayout>
实现代码:
package com.leaf.android; 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.telephony.CellLocation; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.GridView; import android.widget.ImageView; import android.widget.SimpleAdapter; public class Main extends Activity implements OnItemClickListener, OnItemSelectedListener { private ImageView imageView; private GridView gridView; private int[] resIds = new int[] { R.drawable.car1, R.drawable.car2, R.drawable.car3, R.drawable.car4, R.drawable.car5, R.drawable.car6, R.drawable.car7, R.drawable.car8, R.drawable.car9 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) this.findViewById(R.id.gridview); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < resIds.length; i++) { Map<String, Object> cell = new HashMap<String, Object>(); cell.put("imageview", resIds[i]); list.add(cell); } SimpleAdapter simpleAdapter = new SimpleAdapter(Main.this, list, R.layout.cell, new String[] { "imageview" }, new int[] { R.id.imageview }); gridView.setAdapter(simpleAdapter); imageView = (ImageView) this.findViewById(R.id.imageview); gridView.setOnItemSelectedListener(this); gridView.setOnItemClickListener(this); imageView.setImageResource(resIds[0]); } public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub imageView.setImageResource(resIds[position]); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub imageView.setImageResource(resIds[position]); } }
效果: