我有一个自定义卡片列表视图..该视图包含一个imageView和三个按钮..
我想要做的是当我点击一个按钮时,我想设置在imageView对象中保存该按钮的卡的图像..
setSelectedimage()会在视图持有者的最后一张卡片中设置一张而不是点击的卡片。
这是我的getView代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(
R.layout.home_card, parent, false);
viewHolder = new ViewHolder();
view.setTag(viewHolder);
viewHolder.imageView = (ImageView) view
.findViewById(R.id.activity_googlecards_card_imageview);
viewHolder.favIB = (ImageButton) view
.findViewById(R.id.fav_imagebutton);
viewHolder.editIB = (ImageButton) view
.findViewById(R.id.edit_imagebutton);
viewHolder.shareIB = (ImageButton) view
.findViewById(R.id.share_imagebutton);
} else {
viewHolder = (ViewHolder) view.getTag();
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageWidth,imageWidth);
viewHolder.imageView.setLayoutParams(params);
viewHolder.imageView.setTag(position);
OnClickListener myOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
String msg = " clicked";
switch (id) {
case R.id.fav_imagebutton:
msg = "favourite" + msg;
break;
case R.id.edit_imagebutton:
msg = "edit" + msg;
viewHolder = (ViewHolder) view.getTag();
System.out.println(viewHolder.imageView.getTag());
setSelectedImage(viewHolder.imageView);
break;
case R.id.share_imagebutton:
msg = "share" + msg;
break;
default:
break;
}
};
viewHolder.favIB.setOnClickListener(myOnClickListener);
viewHolder.editIB.setOnClickListener(myOnClickListener);
viewHolder.shareIB.setOnClickListener(myOnClickListener);
return view;
}
这是我的viewHolder类:
private static class ViewHolder {
ImageView imageView;
ImageButton favIB;
ImageButton editIB;
ImageButton shareIB;
}
提前致谢,
答案 0 :(得分:2)
点击侦听器中的这一行是问题:
viewHolder = (ViewHolder) view.getTag();
点击期间收到的view
是实际点击的视图,这意味着onClick(View v)
case R.id.edit_imagebutton
的{{1}} View v
是ImageButton editIB
。
所以当你要求它的标签时,没有标签,因为你从未设置过标签。您应该将标记设置为能够检索它。
viewHolder.editIB.setTag(viewHolder);
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.home_card, parent, false);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
viewHolder.imageView = (ImageView) convertView
.findViewById(R.id.activity_googlecards_card_imageview);
viewHolder.favIB = (ImageButton) convertView
.findViewById(R.id.fav_imagebutton);
viewHolder.editIB = (ImageButton) convertView
.findViewById(R.id.edit_imagebutton);
viewHolder.editIB.setTag(viewHolder); // THIS
viewHolder.shareIB = (ImageButton) convertView
.findViewById(R.id.share_imagebutton);
OnClickListener myOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
String msg = " clicked";
switch (id) {
case R.id.fav_imagebutton:
msg = "favourite" + msg;
break;
case R.id.edit_imagebutton:
msg = "edit" + msg;
viewHolder = (ViewHolder) v.getTag(); // HERE
setSelectedImage(viewHolder.imageView);
break;
case R.id.share_imagebutton:
msg = "share" + msg;
break;
default:
break;
}
};
viewHolder.favIB.setOnClickListener(myOnClickListener);
viewHolder.editIB.setOnClickListener(myOnClickListener);
viewHolder.shareIB.setOnClickListener(myOnClickListener);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageWidth,imageWidth);
viewHolder.imageView.setLayoutParams(params);
viewHolder.imageView.setTag(position);
return view;
}
答案 1 :(得分:1)
试试这个:
viewHolder.editIB.setOnClickListener(new OnClickListener() {
ImageView iv = viewHolder.imageView;
@Override
public void onClick(View v) {
//you can use iv here it will get the right image
}
});
答案 2 :(得分:0)
viewHolder.imageView = (ImageView) view
.findViewById(R.id.activity_googlecards_card_imageview);
更改为view.imageView = (ImageView) view
.findViewById(R.id.activity_googlecards_card_imageview);
答案 3 :(得分:0)
你应该使用你的布局的层次结构来获取View。这可能对你有帮助 -
viewHolder.imageView.setTag(position);
OnClickListener myOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
String msg = " clicked";
switch (id) {
case R.id.fav_imagebutton:
msg = "favourite" + msg;
break;
case R.id.edit_imagebutton:
msg = "edit" + msg;
//get sibling view using hierarchy like
EditText et = (EditText)v;
// fetch its parent and cast to class whatever it is RelativeLayout or LinearLayout
// eg. Relativelayout is edittext' parent in layout
RelativeLayout rl = (RelativeLayout)et.getParent();
// Now get its child named activity_googlecards_card_imageview
ImageView imageView = (ImageView) rl.findViewById(R.id.activity_googlecards_card_imageview);
System.out.println(imageView.getTag());
setSelectedImage(imageView);
break;
case R.id.share_imagebutton:
msg = "share" + msg;
break;
default:
break;
}
};
viewHolder.favIB.setOnClickListener(myOnClickListener);
viewHolder.editIB.setOnClickListener(myOnClickListener);
viewHolder.shareIB.setOnClickListener(myOnClickListener);