我有一个列表适配器,在填充列表视图时为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侦听器。提前致谢。
答案 0 :(得分:3)
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;
}
答案 1 :(得分:0)
Drawable drawable1 =getContext().getResources().getDrawable(R.drawable.add);
Drawable drawable2 =getContext().getResources().getDrawable(R.drawable.checked);
如果有人想知道,我是如何解决双击问题的,我只是声明并将两个图像drawables初始化为全局变量。我不知道为什么会有效,但确实如此。我没有足够的评论声誉,所以我给它作为答案。在此之前,我尝试更改xml状态以及所有这些,但没有一个工作。