我正在尝试在listView中的项目上设置和onClick侦听器但是出于某种原因,单击它们时从不出现toast。我不确定在这个例子中到底发生了什么,但是非常感谢任何建议。
public class CustomListViewAdapter extends ArrayAdapter<Cmd> {
Activity context;
List<Cmd> videos;
public CustomListViewAdapter(Activity context, List<Cmd> videos) {
super(context, R.layout.list_item2, videos);
this.context = context;
this.videos = videos;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
public Cmd getItem(int position) {
return videos.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtCmd.setText(cmd.getCmd());
// holder.imageView.setImageBitmap(cmd.getImageBitmap());
holder.txtPrice.setText(cmd.getVideoURL() + "");
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
}
});
return convertView;
}
}
答案 0 :(得分:0)
使您的ImageView可点击且可对焦
答案 1 :(得分:0)
您可以在几个地方改进ViewHolder
模式的使用。首先,每次调用getView时都会重置onClickListener()
。您应该进行以下更改:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView == null){
view = context.getLayoutInflater().inflate(R.layout.list_item2, null);
final ViewHolder holder = new ViewHolder();
holder.txtSuccess = (TextView) view
.findViewById(R.id.success);
holder.txtCmd = (TextView) view.findViewById(R.id.cmd);
holder.txtPrice = (TextView) view.findViewById(R.id.price);
holder.imageView = (ImageView) view.findViewById(R.id.thumbnail);
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
}
});
view.setTag(holder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtCmd.setText(cmd.getCmd());
// holder.imageView.setImageBitmap(cmd.getImageBitmap());
holder.txtPrice.setText(cmd.getVideoURL() + "");
return view;
}
此外,您应该使ViewHolder内部类静态。
/* private view holder class */
private static class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
答案 2 :(得分:-2)
尝试实施触摸,而不是点击或使用ImageButton
代替ImageView
holder.imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
break;
}
}
return true;
}
});