android控件)GridView控件開發總結

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章