我的代码中有一个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时,“点击”不起作用。
谢谢大家!
答案 0 :(得分:2)
问题在于你的布局中的android:clickable =“true”。 当您将ImageButton标记为clickable然后单击GridView项时,控制器将查找ImageButton的单击处理程序而不是GridView项。 因此,永远不会调用您的OnItemClick方法。当您更改布局时,您没有clickable = true。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}