Android之基于BaseAdapter和SimpleAdapter的GridView

 GridView

第一种方式:用SimpleAdapter

先来贴出本文代码运行的结果:
Android之基于BaseAdapter和SimpleAdapter的GridView_第1张图片

本文需要添加/修改3个文件:main.xml、night_item.xml、JAVA源代码。

main.xml源代码如下,本身是个GirdView,用于装载Item:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

介绍一下里面的某些属性:

android:numColumns="auto_fit" ,GridView的列数设置为自动

android:columnWidth="90dp",每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth",缩放与列宽大小同步
android:verticalSpacing="10dp",两行之间的边距,如:行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dp
android:horizontalSpacing="10dp",两列之间的边距。

接下来介绍  night_item.xml,这个XML跟前面ListView的ImageItem.xml很类似:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_height="wrap_content" 
         android:paddingBottom="4dip" android:layout_width="fill_parent">
         <ImageView 
               android:layout_height="wrap_content" 
               android:id="@+id/ItemImage" 
               android:layout_width="wrap_content" 
               android:layout_centerHorizontal="true"> 
         </ImageView>
         <TextView 
               android:layout_width="wrap_content" 
               android:layout_below="@+id/ItemImage" 
               android:layout_height="wrap_content" 
               android:text="TextView01" 
               android:layout_centerHorizontal="true" 
               android:id="@+id/ItemText">
         </TextView>
</RelativeLayout>
最后就是JAVA的源代码了,也跟前面的ListView的JAVA源代码很类似,不过多了“选中”的事件处理:
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        
        //生成动态数组,并且转入数据
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<10;i++)
        {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
			map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText
        	lstImageItem.add(map);
        }
        //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
        SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
        		                                    lstImageItem,//数据来源 
        		                                    R.layout.night_item,//night_item的XML实现
        		                                    
        		                                    //动态数组与ImageItem对应的子项        
        		                                    new String[] {"ItemImage","ItemText"}, 
        		                                    
        		                                    //ImageItem的XML文件里面的一个ImageView,两个TextView ID
        		                                    new int[] {R.id.ItemImage,R.id.ItemText});
        //添加并且显示
        gridview.setAdapter(saImageItems);
        //添加消息处理
        gridview.setOnItemClickListener(new ItemClickListener());
    }
    
    //当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件
    class  ItemClickListener implements OnItemClickListener
    {
		public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened 
				                          View arg1,//The view within the AdapterView that was clicked
				                          int arg2,//The position of the view in the adapter
				                          long arg3//The row id of the item that was clicked
				                          ) {
			//在本例中arg2=arg3
			HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
			//显示所选Item的ItemText
			setTitle((String)item.get("ItemText"));
		}
    	
    }

第二种方式:用BaseAdapter

实现一个继承BaseAdapter的 ImageAdapter
package com.test;  
  
import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.AdapterView;  
import android.widget.BaseAdapter;  
import android.widget.GridView;  
import android.widget.ImageView;  
import android.widget.TextView;  
import android.widget.Toast;  
import android.widget.AdapterView.OnItemClickListener;  
  
public class GridViewActivity extends Activity {  
  
    //存放各功能图片  
    private Integer[] mFunctionPics = {R.drawable.mobilesecurity,R.drawable.infosecurity,  
            R.drawable.no_interrupt,R.drawable.bianjiejieting,R.drawable.xiaomishu  
            ,R.drawable.sms_send_attime,R.drawable.yule,R.drawable.setting};  
    //存放各功能的名称  
    private Integer[] mFunctionName = {R.string.phone_tools_box_name1,  
            R.string.phone_tools_box_name2,R.string.phone_tools_box_name3,  
            R.string.phone_tools_box_name4,R.string.phone_tools_box_name5,  
            R.string.phone_tools_box_name6,R.string.phone_tools_box_name7,  
            R.string.phone_tools_box_name8};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.gridview_main);  
          
        GridView girdview = (GridView) this.findViewById(R.id.girdview);  
        girdview.setAdapter(new ImageAdapter(this));  
        girdview.setOnItemClickListener(new OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> parent, View view,  
                    int position, long id) {  
                Toast.makeText(GridViewActivity.this, "图片ID:"+mFunctionPics[position],1).show();  
            }  
        });  
          
    }  
      
    public class ImageAdapter extends BaseAdapter{  
  
        private Context mContext;  
        private LayoutInflater inflater;  
        private class GirdTemp{  
            ImageView phone_function_pic;  
            TextView phone_function_name;  
        }  
        public ImageAdapter(Context c){  
            mContext = c;  
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        }  
        @Override  
        public int getCount() {  
            return mFunctionPics.length;  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return position;  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            GirdTemp temp;  
            if(convertView == null){  
                convertView = inflater.inflate(R.layout.gridview_item, null);  
                temp = new GirdTemp();  
                temp.phone_function_pic = (ImageView) convertView.findViewById(R.id.function_view);  
                temp.phone_function_name = (TextView) convertView.findViewById(R.id.function_name);  
                convertView.setTag(holder);  
            }else{  
                temp = (GirdHolder) convertView.getTag();  
            }  
            temp.phone_function_pic.setImageResource(mFunctionPics[position]);  
            temp.phone_function_name.setText(mFunctionName[position]);  
            return convertView;  
        }  
          
    }  
  
}  
girdview_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>  
<GridView xmlns:android="http://schemas.android.com/apk/res/android"   
    android:id="@+id/gridview"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"  
    android:numColumns="auto_fit"  //自动适应多少行
    android:verticalSpacing="20dp"  //两行之间的距离
    android:horizontalSpacing="20dp"  //两列之间的距离
    android:gravity="center"  
/> 
girdview_item.xml源码:
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout   
        xmlns:android="http://schemas.android.com/apk/res/android"   
 android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
         <ImageView>   
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
<span style="white-space:pre">		</span>android:id="@+id/function_view"
<span style="white-space:pre">		</span>android:layout_centerHorizontal="true"
<span style="white-space:pre">	</span></ImageView> 
         <TextView   
               android:layout_width="wrap_content"   
               android:layout_below="@+id/function_view"   
               android:layout_height="wrap_content"   
               android:layout_centerHorizontal="true"   
               android:id="@+id/function_name">  
         </TextView>  
</RelativeLayout>  






你可能感兴趣的:(android,GridView)