Android学习之——GridView
背景知识
GridView在Android开发中和ListView一样经常被使用。如我们经常使用的快图浏览,里面就有将图片的布局改为网格(即GridView)的选项。还有约X神器——陌陌的搜索界也是用了GridView。
定义
GridView
is a ViewGroup
that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter
网格视图是一个二维的可滚动的网格中展示网格项的视图集合。网格项使用ListAdapter自动插入到布局中。
Demo
1.使用SimpleAdapter适配
MainActivity
1 package com.johntsai.gridviewdemo; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.AdapterView.OnItemClickListener; 12 import android.widget.GridView; 13 import android.widget.SimpleAdapter; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 private int ids[]={ 18 R.drawable.aaa, 19 R.drawable.bbb, 20 R.drawable.ccc, 21 R.drawable.ddd, 22 R.drawable.eee, 23 R.drawable.fff, 24 R.drawable.ggg, 25 R.drawable.hhh, 26 R.drawable.aaa 27 }; 28 private GridView gridView; 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 gridView = (GridView)findViewById(R.id.gridview); 35 36 ArrayList<HashMap<String, Object>> images = new ArrayList<HashMap<String,Object>>(); 37 for(int i=0;i<ids.length;i++){ 38 HashMap<String, Object>map = new HashMap<String, Object>(); 39 map.put("Image", ids[i]); 40 map.put("Id", "Android"+i+"号"); 41 images.add(map); 42 } 43 44 SimpleAdapter adapter = new SimpleAdapter(this, 45 images, R.layout.gridview_item, 46 new String[]{"Image","Id"}, new int[]{R.id.imageItem,R.id.idItem}); 47 gridView.setAdapter(adapter); 48 49 gridView.setOnItemClickListener(new OnItemClickListener() { 50 51 @Override 52 public void onItemClick(AdapterView<?> parent, View view, 53 int position, long id) { 54 Toast.makeText(getApplicationContext(), 55 "你点击了" + position+"号", Toast.LENGTH_LONG).show(); 56 57 } 58 }); 59 } 60 }
主界面布局文件
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:gravity="center" /> <!-- android:columnWidth="90dp" 列宽设置为90dp android:numColumns="auto_fit"列数设置为自动 android:stretchMode="columnWidth" 缩放与列的宽度同步 -->
GridView项布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="4dip"> 6 7 <ImageView 8 android:id="@+id/imageItem" 9 android:layout_height="70dp" 10 android:layout_width="70dp" 11 android:layout_centerHorizontal="true" 12 /> 13 <TextView 14 android:id="@+id/idItem" 15 android:layout_height="wrap_content" 16 android:layout_width="70dp" 17 android:layout_centerHorizontal="true" 18 android:layout_below="@id/imageItem" 19 /> 20 </RelativeLayout>
运行效果:
2.使用继承自BaseAdapter的ImageAdapter适配
1 package com.johntsai.gridviewdemo; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.GridView; 8 import android.widget.ImageView; 9 10 public class ImageAdapter extends BaseAdapter { 11 public Integer [] ImagesIds = { 12 R.drawable.aaa, 13 R.drawable.bbb, 14 R.drawable.ccc, 15 R.drawable.ddd, 16 R.drawable.eee, 17 R.drawable.fff, 18 R.drawable.ggg, 19 R.drawable.hhh 20 }; 21 private Context context; 22 //构造方法 23 public ImageAdapter(Context c) { 24 context = c; 25 } 26 @Override 27 public int getCount() { 28 return ImagesIds.length; 29 } 30 31 @Override 32 public Object getItem(int position) { 33 return null; 34 } 35 36 @Override 37 public long getItemId(int position) { 38 return 0; 39 } 40 41 @Override 42 public View getView(int position, View convertView, ViewGroup parent) { 43 ImageView imageView; 44 if (convertView == null) { 45 imageView = new ImageView(context); 46 imageView.setLayoutParams(new GridView.LayoutParams(100, 100)); 47 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 48 imageView.setPadding(0, 0, 0, 0); 49 } else { 50 imageView = (ImageView) convertView; 51 } 52 53 imageView.setImageResource(ImagesIds[position]); 54 return imageView; 55 } 56 57 }
MainActivity
1 public class MainActivity extends Activity { 2 private GridView gridView; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 gridView = (GridView)findViewById(R.id.gridview); 9 gridView.setAdapter(new ImageAdapter(getApplicationContext())); 10 11 gridView.setOnItemClickListener(new OnItemClickListener() { 12 13 @Override 14 public void onItemClick(AdapterView<?> parent, View view, 15 int position, long id) { 16 Toast.makeText(getApplicationContext(), 17 "你点击了" + position+"号", Toast.LENGTH_LONG).show(); 18 19 } 20 }); 21 } 22 }