我的OnClickListener不起作用。 MyCode是:
public class ReportAboutProblem extends ActionBarActivity implements View.OnClickListener{
public final int CAMERA_RESULT = 0;
GridView gridView;
Button btnAddPhoto;
ArrayList<Bitmap> myImages;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report_about_problem_activity);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#85abc4")));
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
btnAddPhoto=(Button)findViewById(R.id.btn_add_photo);
btnAddPhoto.setOnClickListener(this);
gridView = (GridView) findViewById(R.id.grid_new);
myImages=new ArrayList<Bitmap>();
myImages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher));
gridView.setOnItemClickListener(mItemClickListener);
gridView.setAdapter(new CustomGridAdapter(this, myImages));
}
private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(ReportAboutProblem.this, "You have clicked on item '" + "'", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_RESULT) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
myImages.add(thumbnail);
gridView.setOnItemClickListener(mItemClickListener);
gridView.setAdapter(new CustomGridAdapter(this, myImages));
}
}
}
我的适配器是:
public class CustomGridAdapter extends BaseAdapter {
private Context context;
//private final String[] gridValues;
ArrayList<Bitmap> arrayList;
ImageButton i1;
//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob) {
arrayList = ob;
this.context = context;
// this.gridValues = gridValues;
}
@Override
public int getCount() {
// Number of times getView method call depends upon gridValues.length
return arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
// Number of times getView method call depends upon gridValues.length
public View getView( int position, View convertView, ViewGroup parent) {
final int pos = position;
Log.d("myLogs","position="+position);
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid_item, null);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
Log.d("myLogs","Set image "+position+" image "+arrayList.get(position));
imageView.setImageBitmap(arrayList.get(position));
} else {
gridView = (View) convertView;
}
return gridView;
}
}
自定义gridView元素是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10px"
android:src="@drawable/ic_launcher" android:layout_alignParentLeft="true" android:layout_marginLeft="4dp"
android:layout_alignParentTop="true" android:layout_marginTop="4dp">
</ImageView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton" android:src="@android:drawable/ic_delete"
android:background="@android:color/transparent"
android:paddingLeft="10dp"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"/>
答案 0 :(得分:2)
使用imageView代替ImageButton它将运行..
[这个问题的解决方案是;
不要在网格中使用可点击的对象。那种情况下Android无法处理网格的点击事件。
而是使用某些东西来显示类似的用户界面视图。处理该对象单击操作。
假;将按钮放在网格中以执行一些单击操作。
真;放置一个imageview而不是imagebutton并处理imageview的点击事件。] 1
或强>
放这些
android:focusable="false"
android:focusableInTouchMode="false"
到你的ImageButton。
答案 1 :(得分:1)
尝试使用android:descendantFocusability="blocksDescendants"
,当单击列表项时,ImageButton会消耗焦点。