gridview小练习

来源:互联网 发布:pr转场特效插件 mac 编辑:程序博客网 时间:2024/05/10 13:48
1.主布局的gridview
  1. <GridView
  2.         android:id="@+id/grid"
  3.         android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent"
  5.         android:numColumns="3"
  6.         ></GridView>
复制代码
2.gridview_item的布局
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:orientation="vertical" >
  5.     <ImageView 
  6.         android:id="@+id/item_img"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         />
  10.         <TextView 
  11.             android:id="@+id/item_text"
  12.             android:layout_width="fill_parent"
  13.         android:layout_height="fill_parent"
  14.             />    
  15. </LinearLayout>
复制代码
3.java代码
(1)在MainActivity中声明GridView就不说啦
(2)和布局连接起来
  1. gridView = (GridView)findViewById(R.id.grid);
复制代码
(3)有了V之后还要有M,也就是数据,
  1. List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
  2.                 for (int i = 0; i < 5; i++) {
  3.                         Map<String, Object> item = new HashMap<String, Object>();
  4.                         item.put("imageItem", R.drawable.ic_launcher);
  5.                         item.put("textItem", i);
  6.                         items.add(item);
  7.                 }
复制代码
(4)然后用一个adpter把之前的这些数据啊,item的布局啊包起来。
  1. SimpleAdapter simpleAdapter = new SimpleAdapter(this,
  2.                                 items,//包含数据内容的List
  3.                                 R.layout.gridview_item,
  4.                                 new String[]{"imageItem","textItem"},
  5.                                 new int []{R.id.item_img,R.id.item_text});
复制代码
构造方法格式如下
  1. SimpleAdapter(Context context, 
  2. List<? extends Map<String, ?>> data, 
  3. int resource, 
  4. String[] from,//<span style="color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: rgb(255, 255, 255);">对应之前List的列名,</span>
  5. int[] to)//对应上面列明的<span style="background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;">item中包含的各个子布局</span>
复制代码
(5)然后就是把他们连起来
  1. gridView.setAdapter(simpleAdapter);
复制代码
这样应该就可以显示出来了。(6)再加一个对点击的响应事件
  1. gridView.setOnItemClickListener(new OnItemClickListener() {
  2.                         @Override
  3.                         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  4.                                         long arg3) {
  5.                                 // TODO Auto-generated method stub
  6.                                 String string = gridView.getAdapter().getItem(arg2).toString();
  7.                                 Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
  8.                         }
  9.                 });
复制代码
里面用了了个getItem(int position)方法,其实还有很多其他的方法,getCount(),getItemId(int position),getItemViewType(int position),getView(int position, View convertView, ViewGroup parent),getViewTypeCount(),hasStableIds()等

另外,通用的做法是写一个类继承BaseAdapter。。。下次再说吧~


0 0
原创粉丝点击