我的代码中有一个 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 放入“自定义适配器”时,“点击”不起作用。
谢谢你们!