OnItemClickListener dont't捕獲事件點擊我有,有一個適配器視圖中顯示的ImageViewAndroid的 - 在適配器視圖
的gridview的活動的活動:
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都有自己的點擊處理程序來切換複選標記,所以姿勢被消耗的複選框,並永遠不會到達你的適配器視圖。 – Sam 2013-02-27 23:16:33
Hi Sam, 謝謝你的迴應。 據我所知,我們無法從類活動訪問CheckBox適配器類,對嗎?你會在適配器中捕獲它,然後通知活動類嗎? 對不起,我是Java新手。 – fgallego 2013-02-28 07:41:13