android控件)GridView控件开发总结

来源:互联网 发布:js事件代理和事件委托 编辑:程序博客网 时间:2024/06/08 04:57

1 界面

<GridView
                      android:listSelector="#FAF4FF"
                      android:id="@+id/GridView"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:columnWidth="140dp"
                      android:padding="0dp"
                      android:numColumns="2"
                      android:stretchMode="columnWidth" >

                </GridView>

2 griditem 界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/itemselect"
android:layout_height="wrap_content"
android:padding="10dp"
>
<ImageView
    android:layout_width="130dp"
    android:background="@drawable/imgbar"
    android:id="@+id/ItemImage"
    android:adjustViewBounds="true"
    android:padding="1dp"
    android:layout_height="108dp"
    android:layout_centerHorizontal="true"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="27dp"
    android:layout_marginBottom="10dp"
    android:layout_below="@+id/ItemImage"
    android:textColor="#000000"
    android:textSize="19sp"
    android:id="@+id/ItemText"
    android:layout_centerHorizontal="true"
/>

</RelativeLayout>



3 后台数据绑定:

ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();

        
HashMap map = new HashMap<String, Object>();
          map.put("ItemImage", R.drawable.f11);
          map.put("ItemText", "松鼠桂鱼");
          meumList.add(map);
           
        SimpleAdapter saItem = new SimpleAdapter(this,
                  meumList, //数据源
                  R.layout.griditem, //xml实现
                  new String[]{"ItemImage","ItemText"}, //对应map的Key
                  new int[]{R.id.ItemImage,R.id.ItemText});  //对应R的Id
 
                //添加Item到网格中
                gridview.setAdapter(saItem);
                //添加点击事件
                gridview.setOnItemClickListener(
                    new OnItemClickListener()
                    {
                      

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
                            
                                Intent intent = new Intent();
                                intent.putExtra("index",arg2);
                                intent.setClass(Main.this, Detail.class);
                                startActivity(intent);
                            
                        }
                    }

                );


原文地址:http://www.cnblogs.com/macroxu-1982/archive/2012/06/06/2538127.html

原创粉丝点击