我正在开发简单的 feedreader 应用程序。在我的主要活动中,我有显示所有项目的列表视图。在每一行中,我都有用于保存该项目的图像按钮,当用户单击该图像按钮的图像必须更改时(如 twitter 上的收藏图标),但它不会更改!滚动后观察到变化!
这是我的行布局。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="@+id/tvListViewItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:text="@string/listview_item_title"
android:textSize="25dp" />
<TextView
android:id="@+id/tvListViewItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/tvListViewItemTitle"
android:layout_below="@+id/tvListViewItemTitle"
android:layout_marginTop="20dp"
android:text="@string/listview_item_text"
android:textSize="18dp" />
<TextView
android:id="@+id/tvListViewItemTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/tvListViewItemText"
android:layout_below="@+id/tvListViewItemText"
android:layout_marginTop="22dp"
android:paddingTop="5dp"
android:text="@string/listview_item_time" />
<ImageButton
android:id="@+id/ibListViewItemSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/tvListViewItemTime"
android:layout_marginLeft="20dp"
android:paddingTop="11dp"
android:src="@drawable/icon_save_story_2x"
android:background="@null" />
这是我的自定义适配器类。
public class FeedListAdapter extends ArrayAdapter<FeedItem> {
private final Activity context;
private final List<FeedItem> list;
public FeedListAdapter(Activity context, List<FeedItem> list) {
super(context, R.layout.feed_listview_item_type1, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
TextView tvTitle;
TextView tvText;
TextView tvTime;
ImageButton ibSave;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.feed_listview_item_type1, parent,false);
// configure view holder
ViewHolder holder = new ViewHolder();
holder.tvTitle = (TextView)rowView.findViewById(R.id.tvListViewItemTitle);
holder.tvText = (TextView)rowView.findViewById(R.id.tvListViewItemText);
holder.tvTime = (TextView)rowView.findViewById(R.id.tvListViewItemTime);
holder.ibSave = (ImageButton)rowView.findViewById(R.id.ibListViewItemSave);
rowView.setTag(holder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
final FeedItem feedItem = list.get(position);
holder.tvTitle.setText(feedItem.getTitle());
holder.tvText.setText(feedItem.getText());
holder.tvTime.setText(feedItem.getTime());
if(feedItem.isSave()) holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
else holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
final String msg = " "+(position+1);
holder.ibSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(feedItem.isSave()){
feedItem.setSave(false);
Toast.makeText(context,
msg+"deleted", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
}else{
feedItem.setSave(true);
Toast.makeText(context,
msg+"saved", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
}
}
});
return rowView;
}
}