gpt4 book ai didi

Android - OnItemClickListener 不捕获适配器 View 中的事件点击

转载 作者:行者123 更新时间:2023-11-30 03:45:11 25 4
gpt4 key购买 nike

我有一个 Activity ,它有一个 AdapterView 来显示 ImageView 的 GridView

Activity :

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 中单击,事件单击不会触发。

我做错了什么?我需要做什么?

提前致谢。

最佳答案

android:focusableInTouchMode="false"android:focusable="false"

关于Android - OnItemClickListener 不捕获适配器 View 中的事件点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15124207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com