首页 > 代码库 > GridView视图(BaseAdapter)

GridView视图(BaseAdapter)

效果图:

技术分享

1.src文件夹下 ShowActivity.java文件内容:

 

package com.jun.activity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import com.jun.adapter.MyAdapter;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.GridView;import android.widget.SimpleAdapter;public class ShowActivity extends Activity {    private GridView gridView=null;    private MyAdapter adapter=null;    private SimpleAdapter adapter1=null;    private List<HashMap<String,Object>> list=null;    private HashMap<String,Object> map=null;    private String data[]={"图片1","图片2","图片3","图片4","图片5","图6","图片7","图片8","图片9",            "图片10","图片11","图片12","图片13","图片14","图片15","图片16"};    private int   imgId[]={R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,            R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,            R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a};    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_show);        init();        setData();    }private void init(){        gridView=(GridView) findViewById(R.id.grid_view);    list=new ArrayList<HashMap<String,Object>>();}private void setData(){//    使用BaseAdapter添加数据    adapter=new MyAdapter(ShowActivity.this, data, imgId);        gridView.setAdapter(adapter);            //    使用SimpeAdapter添加数据//    for(int i=0;i<data.length;i++){//        //        map=new HashMap<String, Object>();//        //        map.put("text", data[i]);//        map.put("img", imgId[i]);//        list.add(map);//        //    }//    adapter1=new SimpleAdapter(ShowActivity.this, list, R.layout.gridview_item, new String[]{"text","img"}, new int[]{R.id.gridview_text,R.id.gridview_img});//    gridView.setAdapter(adapter1);}    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_show, menu);        return true;    }}

 

2.ShowActivity布局文件activity_show.xml文件内容:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/activity_background" >    <TextView        android:id="@+id/title_bar_text"        android:layout_width="match_parent"        android:layout_height="45dip"        android:layout_alignParentTop="true"        android:gravity="center_vertical|center_horizontal"        android:background="@drawable/theme_title_red"        android:text="@string/test"        android:textSize="20dip"        android:textColor="#fff"        tools:context=".ShowActivity" />    <GridView        android:id="@+id/grid_view"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/title_bar_text"        android:layout_marginLeft="1dip"        android:layout_marginRight="1dip"        android:numColumns="2"       android:columnWidth="5dip"        android:cacheColorHint="#00000000"        ></GridView>        </RelativeLayout>

 

3.继承自BaseAdapter适配器类MyAdapter.java文件内容:

 

package com.jun.adapter;import com.jun.activity.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter {    private String data[]=null;    private int imgId[]=null;    private Context context=null;    private LayoutInflater inflater=null;    public MyAdapter(Context context,String[] data, int[] imgId) {        super();        this.data =http://www.mamicode.com/ data;        this.imgId = imgId;        this.context = context;                inflater=LayoutInflater.from(context);    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return data.length;    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    private class Holder{                TextView tv=null;        ImageView img=null;        public TextView getTv() {            return tv;        }        public void setTv(TextView tv) {            this.tv = tv;        }        public ImageView getImg() {            return img;        }        public void setImg(ImageView img) {            this.img = img;        }            }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub//        获得holder以及holder对象中tv和img对象的实例        Holder holder;        if(convertView==null){                                    convertView=inflater.inflate(R.layout.gridview_item, null);            holder=new Holder();            holder.tv=(TextView) convertView.findViewById(R.id.gridview_text);            holder.img=(ImageView) convertView.findViewById(R.id.gridview_img);                        convertView.setTag(holder);                    }else{            holder=(Holder) convertView.getTag();                    }//        为holder中的tv和img设置内容        holder.tv.setText(data[position]);        holder.img.setImageResource(imgId[position]);//        注意  默认为返回null,必须得返回convertView视图        return convertView;    }}

 

4.GridView组件上面的布局:   gridview_item.xml文件内容:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/activity_background" ><ImageView            android:id="@+id/gridview_img"            android:layout_width="120dip"            android:layout_height="120dip"            android:layout_margin="10dip"            android:background="@drawable/four_bg"            android:src="@drawable/a" />    <TextView        android:id="@+id/gridview_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/gridview_img"        android:gravity="center_vertical|center_horizontal"        android:textColor="#000"        android:textSize="14dip"        tools:context=".ShowActivity" /></RelativeLayout>

 

GridView视图(BaseAdapter)