一. 和运用listview一样,首先需要在onCreate方法中,查找并初始化视图。
二. 在onCreate 方法中,初始化一个adapter,关键是在初始化adapter时,用到了构造方法,这个构造方法是带3个参数的,其中第一个参数,是把当前content即activity,代入到了adapter视图中,帮助adapter视图完成查找解析资源文件的功能。
这段代码是在主activity中的。即gridview中的。
adapter = new MainAdapter(this, names, icons);
这段代码是在adapter中的,即baseadapter中的,其中在View.inflate的语句中,context是从构造方法中传入的上下文。
@Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null) { convertView = View.inflate(context, R.layout.item_main, null); } ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon); TextView textView = (TextView) convertView.findViewById(R.id.tv_item_name); imageView.setImageResource(icons[position]); textView.setText(names[position]); return convertView; }
三.大体步骤为,a)初始化gridview,b)初始化adapter,c)设置GridView的adpater为新new出来的adapter,d)设置监听事件setOnItemClickListener
以下为完整代码
package com.atguigu.app03_gridview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class MainActivity extends Activity { private GridView gv_main; private MainAdapter adapter; String[] names = new String[] { "手机防盗", "通讯卫士", "软件管理", "流量管理", "进程管理", "手机杀毒", "缓存清理", "高级工具", "设置中心" }; int[] icons = new int[] { R.drawable.widget01, R.drawable.widget02, R.drawable.widget03, R.drawable.widget04, R.drawable.widget05, R.drawable.widget06, R.drawable.widget07, R.drawable.widget08, R.drawable.widget09 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gv_main = (GridView) findViewById(R.id.gv_main); adapter = new MainAdapter(this, names, icons); gv_main.setAdapter(adapter); //给gridView的Item设置点击监听 gv_main.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //得到当前点击的名称 String name = names[position]; //提示 Toast.makeText(MainActivity.this, name, 1).show(); } }); } }
package com.atguigu.app03_gridview; import android.content.Context; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MainAdapter extends BaseAdapter { private String[] names; private int[] icons; private Context context; public MainAdapter(Context context, String[] names, int[] icons) { super(); this.context = context; this.names = names; this.icons = icons; } @Override public int getCount() { return names.length; } @Override public Object getItem(int position) { return names[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null) { convertView = View.inflate(context, R.layout.item_main, null); } ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon); TextView textView = (TextView) convertView.findViewById(R.id.tv_item_name); imageView.setImageResource(icons[position]); textView.setText(names[position]); return convertView; } }