GridView
点击GridView中的某项时,只回调了onItemSelected,而没有回调onItemClick,Why????
main.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical" >
-
-
<ImageView
-
android:id="@+id/imageView"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:src="@drawable/ic_launcher"
-
/>
-
<GridView
-
android:id="@+id/gridView"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:horizontalSpacing="20dp"
-
android:numColumns="1"
-
android:padding="20dp"
-
android:verticalSpacing="20dp" />
-
-
</LinearLayout>
cell.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical" >
-
<ImageView
-
android:id="@+id/imageViewValue"
-
android:layout_width="48dp"
-
android:layout_height="48dp"
-
/>
-
</LinearLayout>
-
package com.lwb.gridview;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
import com.example.android_gridview.R;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.AdapterView.OnItemSelectedListener;
-
import android.widget.GridView;
-
import android.widget.ImageView;
-
import android.widget.SimpleAdapter;
-
-
public class MainActivity extends Activity implements OnItemSelectedListener,
-
OnItemClickListener{
-
-
private ImageView imageView = null;
-
private GridView gridView = null;
-
private int[] resIds = new int[]{
-
R.drawable.item1,R.drawable.item3,R.drawable.item4,
-
R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,
-
R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12,
-
R.drawable.item13
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
// TODO Auto-generated method stub
-
super.onCreate(savedInstanceState);
-
-
setContentView(R.layout.main);
-
gridView = (GridView)this.findViewById(R.id.gridView);
-
-
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
-
for(int i=0;i<resIds.length;i++){
-
Map<String,Object> cell = new HashMap<String,Object>();
-
cell.put("imageViewKey", resIds[i]);
-
list.add(cell);
-
}
-
-
-
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list,R.layout.cell, new String[]{"imageViewKey"}, new int[]{R.id.imageViewValue});
-
// SimpleAdapter simpleAdapter2= new SimpleAdapter(context, data, resource, from, to)
-
-
gridView.setAdapter(simpleAdapter);
-
-
imageView= (ImageView)this.findViewById(R.id.imageView );
-
gridView.setOnItemClickListener(this);
-
gridView.setOnItemSelectedListener(this);
-
imageView.setImageResource(resIds[0]);
-
}
-
-
@Override
-
public void onItemSelected(AdapterView<?> parent, View view, int position,
-
long id) {
-
// TODO Auto-generated method stub
-
imageView.setImageResource(resIds[position]);
-
System.out.println("onItemSelected" + position);
-
}
-
-
@Override
-
public void onNothingSelected(AdapterView<?> parent) {
-
// TODO Auto-generated method stub
-
-
}
-
-
@Override
-
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-
// TODO Auto-generated method stub
-
imageView.setImageResource(resIds[position]);
-
System.out.println("onItemSelected" + position);
-
}
-
-
}
阅读(1418) | 评论(0) | 转发(0) |