我有一个有 AdapterView 的活动来显示 ImageView 的网格视图
活动:
package com.xlck.mislistas
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.xlck.mislistas.adapters.ExpandableHeightGridView;
import com.xlck.mislistas.adapters.ImageGridAdapter;
import com.xlck.mislistas.adapters.ImageGridAdapter.ViewHolder;
import com.xlck.mislistas.adapters.ImageGridBean;
public class AmigosActivity extends SherlockActivity {
...
private ExpandableHeightGridView gridViewImagenes;
gridViewImagenes = (ExpandableHeightGridView) findViewById(R.id.grvImagenes);
// Adapter GridView
gridViewImagenes.setAdapter(imageGridAdapter);
gridViewImagenes.setExpanded(true);
.
.
.
// Listener
gridViewImagenes.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Do something ...;
}
});
}
适配器:
package com.xlck.mislistas.adapters;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.xlck.mislistas.R;
public class ImageGridAdapter extends BaseAdapter {
private Context mContext;
private List<ImageGridBean> items;
// Constructor
public ImageGridAdapter(Context context, ArrayList<ImageGridBean> items) {
this.mContext = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ImageGridBean item = (ImageGridBean) items.get(position);
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_grid_imagen, null);
holder = new ViewHolder();
holder.txtId = (TextView) convertView.findViewById(R.id.uid);
holder.txtNombre = (TextView) convertView
.findViewById(R.id.txtNombre);
holder.imagen = (ImageView) convertView
.findViewById(R.id.imgImagen);
holder.check = (CheckBox) convertView.findViewById(R.id.chkItem);
holder.txtFondoNombre = (TextView) convertView.findViewById(R.id.txtFondoNombre);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtId.setText(item.getId());
holder.txtNombre.setText(item.getNombre());
holder.imagen.setImageBitmap(item.getImagen());
if (item.getId().equals("0"))
holder.check.setVisibility(View.INVISIBLE);
return convertView;
}
@Override
public ImageGridBean getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
// --------------------------------------------------------< ViewHolder >---
// -------------------------------------------------------------------------
/* private view holder class */
public class ViewHolder {
public TextView txtId;
public TextView txtNombre;
public TextView txtFondoNombre;
public ImageView imagen;
public CheckBox check;
}
}
我有这个布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="92dp"
android:layout_height="92dp"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="false" >
<TextView
.../>
<ImageView
.../>
<TextView
.../>
<TextView
... />
</RelativeLayout>
<LinearLayout
android:layout_width="38dp"
android:layout_height="32dp"
android:layout_alignRight="@+id/relativeLayout2"
android:layout_alignTop="@+id/relativeLayout2"
android:layout_marginRight="0dp"
android:layout_marginTop="4dp" >
<CheckBox
android:id="@+id/chkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
好吧,如果我在 ImageView 中单击,它会触发事件单击并且侦听器会捕获它,但是如果我在 CheckBox 中单击,则不会触发事件单击。
我做错了什么?我需要做什么?
提前致谢。