[英]Change image resource on click of an image button in a list view
我有一个列表适配器,用于在填充列表视图时为ImageButtons设置onClick侦听器。 我正在尝试使图像资源更改onClick。 我在onClick侦听器中检索图像资源时遇到问题。
列表适配器:
public class ListAdapter extends BaseAdapter {
Context context;
protected List<Post> cityFeed;
LayoutInflater inflater;
public ListAdapter(Context context, List<Post> cityFeed) {
this.cityFeed = cityFeed;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return cityFeed.size();
}
public Post getItem(int position) {
return cityFeed.get(position);
}
public long getItemId(int position) {
return cityFeed.get(position).getDrawableID();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// inflate the list item
convertView = this.inflater.inflate(R.layout.row_layout, parent, false);
// get views
holder.profilePic = (ImageView) convertView.findViewById(R.id.profilePic);
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.day = (TextView) convertView.findViewById(R.id.day);
holder.rating = (TextView) convertView.findViewById(R.id.rating);
holder.textPost = (TextView) convertView.findViewById(R.id.textPost);
holder.ratingUp = (ImageButton) convertView.findViewById(R.id.ratingUp);
holder.ratingDown = (ImageButton) convertView.findViewById(R.id.ratingDown);
convertView.setTag(holder);
holder.ratingUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View convertView) {
// if image source equals this drawable then...
// else change the image source to this drawable...
Toast.makeText( context,
"Rate Up",
Toast.LENGTH_SHORT).show();
}
});
holder.ratingDown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View convertView) {
Toast.makeText( context,
"Rate Down",
Toast.LENGTH_SHORT).show();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Post post = cityFeed.get(position);
holder.profilePic.setImageResource(post.getDrawableID());
holder.username.setText(post.getUsername());
holder.day.setText(post.getDay());
holder.rating.setText(post.getRating());
holder.textPost.setText(post.getText());
return convertView;
}
private class ViewHolder {
ImageView profilePic;
TextView username;
TextView day;
TextView rating;
TextView textPost;
ImageView ratingUp;
ImageView ratingDown;
}
敬酒工作正常,但我不知道如何处理图像资源。 请参阅getView方法中的onClick侦听器。 提前致谢。
Drawable myDrawable1= //get your drawable 1 here
Drawable myDrawable2= //get your drawable 2 here
if ((Post)getItem(pos).isRatedUp){
holder.ratingUp.setImageDrawable(myDrawable2);
}
else{
holder.ratingUp.setImageDrawable(myDrawable1);
}
holder.ratingUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View convertView) {
// if image source equals this drawable then...
// else change the image source to this drawable...
if(((ImageButton)convertView).getDrawable()==myDrawable1){
((ImageButton)convertView).setImageDrawable(myDrawable2);
((Post)getItem(pos)).setRatedUp(true);
}else{
((ImageButton)convertView).setImageDrawable(myDrawable1);
((Post)getItem(pos)).setRatedUp(false);
}
}
});
将此添加到您的Post.class;
private boolean isRatedUp=false;
public boolean isRatedUp(){
return this.isRatedUp;
}
public void setRatedUp(boolean israted){
this.isRatedUp=israted;
}
Drawable drawable1 =getContext().getResources().getDrawable(R.drawable.add);
Drawable drawable2 =getContext().getResources().getDrawable(R.drawable.checked);
如果有人想知道,我该如何解决双击问题,我只是将两个图像可绘制对象声明并初始化为全局变量。 我不知道它为什么起作用,但是它起作用了。 我没有足够的名声来发表评论,因此,我将其作为答复。 在此之前,我尝试更改xml状态以及所有其他状态,但是没有一个起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.