0

我的代码中有一个 gridview 和一个自定义适配器。我正在尝试设置一个 setOnItemClickedListener 但我没有回报。

有人可以帮我理解为什么我的 setOnItemClickListener 不起作用吗?

谢谢!

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import br.com.m.bpuzzle.data.Card;
import br.com.m.bpuzzle.data.ImageLevel;
import br.com.m.bpuzzle.data.PuzzleAdapter;
import br.com.m.bpuzzle.util.Constantes;

public class BPuzzleActivity extends Activity {

    private GridView puzzle;
    private ImageLevel puzzleLevel;
    private PuzzleAdapter adapter;
    private static BPuzzleActivity context;
    private static ArrayList<Card> itens;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        context = this;
        setupPuzzle();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        context.overridePendingTransition(R.anim.anim__fadeout, R.anim.anim__fadein);
    }

    private void setupPuzzle() {
        Log.i(Constantes.APPTAG, "Setup puzzle");

        puzzleLevel = new ImageLevel(8, ImageLevel.LEVEL_ANIMALS);
        itens = puzzleLevel.getListElements();

        adapter = new PuzzleAdapter(itens, context);

        puzzle = (GridView) findViewById(R.id.puzzle);
        puzzle.setAdapter(adapter);
        puzzle.setClickable(true);

        puzzle.setOnItemClickListener(new ItemGridViewClickListener());
    }

    private static class ItemGridViewClickListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Card item = itens.get(position);
            Toast.makeText(context, "Card: " + item.getCardID(), Toast.LENGTH_LONG).show();
        }

    }

我的布局(gridview 的项目)

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/card"
    android:clickable="true"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:focusableInTouchMode="false" />

适配器

public class PuzzleAdapter extends BaseAdapter {

    private ArrayList<Card> puzzleLevel;
    public static Context context;

    public PuzzleAdapter(Context ctx, ArrayList<Card> puzzleData) {
        context = ctx;
        puzzleLevel = puzzleData;
    }

    @Override
    public int getCount() {
        return this.puzzleLevel.size();
    }

    @Override
    public Card getItem(int position) {
        return puzzleLevel.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageButton image;
        if (convertView == null) {
            image = new ImageButton(context);
            convertView = LayoutInflater.from(context).inflate(R.layout.grid__item, null);
        } else {
            image = (ImageButton) convertView;
        }

        image.setImageResource(Constantes.HIDECARD_RESOURCE);
        image.setLayoutParams(new GridView.LayoutParams(130, 130));
        image.setBackgroundColor(Color.TRANSPARENT);
        return image;
    }

}

更新

我将布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_button"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:src="@drawable/card" />

</LinearLayout>

一切正常。出于某种原因,当我将 ImageButton 放入“自定义适配器”时,“点击”不起作用。

谢谢你们!

4

3 回答 3

2

问题在于布局中的 android:clickable="true" 。当您将 ImageButton 标记为可单击然后单击 GridView 项时,控制器会查找 ImageButton 而不是 GridView 项的单击处理程序。因此,您的 OnItemClick 方法永远不会被调用。当您更改布局时,您没有 clickable=true。

于 2013-01-30T18:12:47.867 回答
1

从不使用gridview,但我通常这样设置我的适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageButton image;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView  = mInflater.inflate(R.layout.grid__item, null);
    }

    image = (ImageButton)  convertView.findViewById(R.id.image);
    image.setImageResource(Constantes.HIDECARD_RESOURCE);
    image.setLayoutParams(new GridView.LayoutParams(130, 130));
    image.setBackgroundColor(Color.TRANSPARENT);
    return convertView;
}
于 2012-07-18T02:07:35.193 回答
1

这适用于我的情况

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(myImages, myViews));
        sdcardImages.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v,
                    int position, long id) {
                Intent i = new Intent(EquipmentViews.this, VIRDemo.class);
                i.putExtra("EquipmentId", equipmentIds[position]);
                startActivity(i);

            }
        });

适配器类

/**
 * Adapter for our image files.
 */
private class ImageAdapter extends BaseAdapter {

    private int[] myRemoteImages;
    private String[] myRemoteViews;

    public ImageAdapter(int[] myimages, String[] myviews) {
        myRemoteImages = myimages;
        myRemoteViews = myviews;
    }

    public int getCount() {
        return this.myRemoteImages.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.imageitem, null);
            TextView tv = (TextView) v.findViewById(R.id.icon_text);
            tv.setText(myRemoteViews[position]);
            ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
            iv.setImageResource(myRemoteImages[position]);

        } else {
            v = convertView;
        }
        return v;
    }
于 2012-07-18T06:23:03.653 回答