Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207862
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-07-03 22:12:24

GridView

点击GridView中的某项时,只回调了onItemSelected,而没有回调onItemClick,Why????
main.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <ImageView
  7.         android:id="@+id/imageView"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:src="@drawable/ic_launcher"
  11.         />
  12.     <GridView
  13.         android:id="@+id/gridView"
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="wrap_content"
  16.         android:horizontalSpacing="20dp"
  17.         android:numColumns="1"
  18.         android:padding="20dp"
  19.         android:verticalSpacing="20dp" />

  20. </LinearLayout>
cell.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     <ImageView
  7.         android:id="@+id/imageViewValue"
  8.         android:layout_width="48dp"
  9.         android:layout_height="48dp"
  10.        />
  11. </LinearLayout>

点击(此处)折叠或打开

  1. package com.lwb.gridview;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import com.example.android_gridview.R;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.AdapterView.OnItemSelectedListener;
  13. import android.widget.GridView;
  14. import android.widget.ImageView;
  15. import android.widget.SimpleAdapter;

  16. public class MainActivity extends Activity implements OnItemSelectedListener,
  17. OnItemClickListener{

  18.     private ImageView imageView = null;
  19.     private GridView gridView = null;
  20.     private int[] resIds = new int[]{
  21.             R.drawable.item1,R.drawable.item3,R.drawable.item4,
  22.             R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,
  23.             R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12,
  24.             R.drawable.item13
  25.     };
  26.     
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         // TODO Auto-generated method stub
  30.         super.onCreate(savedInstanceState);

  31.         setContentView(R.layout.main);
  32.         gridView = (GridView)this.findViewById(R.id.gridView);

  33.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  34.         for(int i=0;i<resIds.length;i++){
  35.             Map<String,Object> cell = new HashMap<String,Object>();
  36.             cell.put("imageViewKey", resIds[i]);
  37.             list.add(cell);
  38.         }
  39.         

  40.         SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list,R.layout.cell, new String[]{"imageViewKey"}, new int[]{R.id.imageViewValue});
  41. //        SimpleAdapter simpleAdapter2= new SimpleAdapter(context, data, resource, from, to)

  42.         gridView.setAdapter(simpleAdapter);

  43.         imageView= (ImageView)this.findViewById(R.id.imageView );
  44.         gridView.setOnItemClickListener(this);
  45.         gridView.setOnItemSelectedListener(this);
  46.         imageView.setImageResource(resIds[0]);
  47.     }

  48.     @Override
  49.     public void onItemSelected(AdapterView<?> parent, View view, int position,
  50.             long id) {
  51.         // TODO Auto-generated method stub
  52.         imageView.setImageResource(resIds[position]);
  53.         System.out.println("onItemSelected" + position);
  54.     }

  55.     @Override
  56.     public void onNothingSelected(AdapterView<?> parent) {
  57.         // TODO Auto-generated method stub
  58.         
  59.     }

  60.     @Override
  61.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  62.         // TODO Auto-generated method stub
  63.         imageView.setImageResource(resIds[position]);
  64.         System.out.println("onItemSelected" + position);
  65.     }

  66. }



阅读(1409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~