gpt4 book ai didi

java - Android 中的 GridView 中未触发 OnItemClick 事件

转载 作者:行者123 更新时间:2023-11-29 09:46:01 25 4
gpt4 key购买 nike

我正在开发一个应用程序,其中 gridview 包含按钮列表...

当我在 gridview 中放置图像而不是按钮时,onItemClickEvent 会被触发。但是如果我在 gridView 中放置按钮,则单击事件不会被调用...我不知道是什么问题...即使我没有异常。 .

这是我的代码...

public class MainMenu extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



GridView gridview = (GridView) findViewById(R.id.mainMenu);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(MainMenu.this, "hello" + position, Toast.LENGTH_SHORT).show();
}
});



}
//inner class for adapter

class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c)
{
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

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

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;

if (convertView == null) { // if it's not recycled, initialize some attributes

btn=new Button(mContext);
// imageView = new ImageView(mContext);

btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));


//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)

// imageView.setPadding(8,8, 8, 8);

} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}

// references to our images
private Integer[] mThumbIds =
{
R.drawable.pantrylocator_icon,
R.drawable.volunteeropportunity_icon,
R.drawable.volunteerlocator_icon,
R.drawable.volunteermanagement_icon,
R.drawable.donationform_icon,
R.drawable.donationviamsg_icon,
R.drawable.donationvideo_icon,
R.drawable.virtualfooddrive_icon,
R.drawable.newevent_icon,
R.drawable.pressrelease_icon,
R.drawable.volunteerphotos_icon,
R.drawable.aboutus_icon,

};
}
}

最佳答案

按钮有自己的 OnClickListener:

 public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;

if (convertView == null) { // if it's not recycled, initialize some attributes

btn=new Button(mContext);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
// imageView = new ImageView(mContext);

btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));


//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)

// imageView.setPadding(8,8, 8, 8);

} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}

关于java - Android 中的 GridView 中未触发 OnItemClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5577945/

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