首页 > 代码库 > gridview
gridview
http://mobile.51cto.com/android-259861.htm
实现第一点好办,添加一个标志位boolean bl来控制按钮的Enable状态,实现第二点就是将View.onClickListener添加到List列表中
1、MyAdapter.java继承自BaseAdapter
- public class MyAdapter extends BaseAdapter{
- private Context context;
- private List<Map<String,Object>> list;
- private LayoutInflater mInflater;
- public MyAdapter(Context context,List<Map<String,Object>> list){
- this.context=context;
- this.list=list;
- mInflater=LayoutInflater.from(this.context);
- }
- public int getCount() {
- // TODO Auto-generated method stub
- if(list!=null)
- return list.size();
- else
- return 0;
- }
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- if(list!=null)
- return list.get(position);
- else
- return null;
- }
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @SuppressWarnings("unused")
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub ViewHolder holder=null;
- if(holder==null){
- holder=new ViewHolder();
- convertView=mInflater.inflate(R.layout.gridview_item, null);
- holder.ib=(ImageButton)convertView.findViewById(R.id.ib);
- convertView.setTag(holder);
- }else{
- holder=(ViewHolder)convertView.getTag();
- }
- //绑定点击事件
- holder.ib.setOnClickListener((OnClickListener) list.get(position).get("listen"));
- //通过标志位控制按钮的Enable
- if(Boolean.parseBoolean(list.get(position).get("bl").toString()))
- holder.ib.setEnabled(true);
- else
- holder.ib.setEnabled(false);
- holder.ib.setImageResource(Integer.parseInt(list.get(position).get("icon").toString()));
- return convertView;
- }
- public final class ViewHolder{
- public ImageButton ib;
- }
- }
2、绑定数据源
- public class Main extends Activity {
- /** Called when the activity is first created. */
- private GridView gvtest;
- private List<Map<String,Object>> list;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findView();
- }
- /*
- * 定义一个标志位bl控制按钮的Enable,同时为了让每个按钮都绑定自有的点击事件提前在数据源中间各自的事件实现
- */
- private void findView(){
- gvtest=(GridView)findViewById(R.id.gvtest);
- list=new ArrayList<Map<String,Object>>();
- Map<String,Object> map;
- map=new HashMap<String,Object>();
- map.put("bl", true);
- map.put("icon", R.drawable.menu_home_dis);
- View.OnClickListener abenClick=new View.OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "You click home menu", Toast.LENGTH_SHORT).show();
- }
- };
- map.put("listen", abenClick);
- list.add(map);
- map=new HashMap<String,Object>();
- map.put("bl", true);
- map.put("icon", R.drawable.menu_backward_dis);
- View.OnClickListener beanClick=new View.OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "You click back menu", Toast.LENGTH_SHORT).show();
- }
- };
- map.put("listen", beanClick);
- list.add(map);
- map=new HashMap<String,Object>();
- map.put("bl", false);
- map.put("icon", R.drawable.menu_forward_dis);
- View.OnClickListener weiboClick=new View.OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "You click forward menu", Toast.LENGTH_SHORT).show();
- }
- };
- map.put("listen", weiboClick);
- list.add(map);
- MyAdapter adapter=new MyAdapter(this, list);
- gvtest.setAdapter(adapter);
- }
- }
3、gridview_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center">
- <ImageButton android:id="@+id/ib"
- android:layout_width="60dip"
- android:layout_height="60dip">
- </ImageButton>
- </LinearLayout>
4、Main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <GridView android:id="@+id/gvtest"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:numColumns="3"
- android:stretchMode="columnWidth"
- android:gravity="center"
- android:layout_gravity="center_horizontal">
- </GridView>
- </LinearLayout>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。