1

我有一个有 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 中单击,则不会触发事件单击。

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

提前致谢。

4

4 回答 4

1

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

于 2013-06-06T15:32:13.947 回答
0

@Sam 是对的,复选框自己处理点击,点击事件永远不会到达 AdapterView。

现在,从技术上讲,您可以android:clickable="false"在布局文件中为您的复选框设置,然后单击事件将传递到视图层次结构中,但在这种情况下,您将无法对复选框做任何有用的事情,它只会始终不加检查。

这是更好的方法。将“做某事”移动到不同的方法中,并为复选框添加另一个 onClickListener,以执行相同的操作:

gridViewImagenes.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        doSomething();
    }
});

myCheckbox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        doSomething();
    }
});

}

private void doSomething() {
    //Do Stuff
}
于 2013-02-28T00:20:04.173 回答
0

认为你将不得不实施 a OnCheckedChangelistenerto the checkbox,在你的情况下 to chkItem

请参阅CompoundButton.OnCheckedChangeListener

实施将类似于...

CheckBox checkBox = (CheckBox) findViewById(R.id.chkItem);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {   
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            // checkbox is checked - doSomething()
        } else {
            // checkbox is unchecked
        }
    }
});

希望这可以帮助。

于 2013-02-28T00:51:32.607 回答
0

最后决定把适配器放到Activity类中。在 Class Adapter 中放置 ImageView 和 CheckBox 组件的侦听器,因为这些侦听器调用 Activity 类的方法。

感谢大家的时间和回复!!!

于 2013-02-28T13:08:16.680 回答