出現這種行爲,因爲ListView
回收行的意見,您滾動列表上下,正因爲如此你行由用戶(圖像發生了變化)所採取行動的圖像應該是未修改的。爲避免這種情況,您必須以某種方式爲列表中的每一行保存ImageView
的狀態,並使用此狀態在getView()
方法中設置正確的圖像。因爲你沒有說明你是如何實現你的適配器的,我將向你展示一個簡單的例子。
首先你應該存儲你的狀態ImageView
。我使用ArrayList<Boolean>
作爲自定義適配器的成員,如果該列表中的位置(對應於列表中的行的位置)爲false
則圖像爲默認圖像,否則如果是true
,則用戶單擊它,我們應該把新形象:
private ArrayList<Boolean> imageStatus = new ArrayList<Boolean>();
在您的自定義適配器的構造函數初始化這個名單。在getView()
方法
//... initialize the imageStatus, objects is the list on which your adapter is based
for (int i = 0; i < objects.size(); i++) {
imageStatus.add(false);
}
然後:例如,如果你把你的適配器的東西名單,那麼你應該讓你的imageStatus
那樣大名單,並充滿false
(默認/啓動狀態)
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.adapters_adapter_with_images, null);
}
// find the image
ImageView favImage = (ImageView) v
.findViewById(R.id.toggle_favorite);
// Set the image bitmap. If the imageStatus flag for this position is TRUE then we
// show the new image because it was previously clicked by the user
if (imageStatus.get(position)) {
int newImage = R.drawable.ic_star_yellow_embossed;
favImage.setImageBitmap(BitmapFactory.decodeResource(
getContext().getResources(), newImage));
} else {
// If the imageStatus is FALSE then we explicitly set the image
// back to the default because we could be dealing with a
// recycled ImageView that has the new image set(there is no need to set a default drawable in the xml layout)
int newImage = R.drawable.basket_empty; //the default image
favImage.setImageBitmap(BitmapFactory.decodeResource(
getContext().getResources(), newImage));
}
// when clicked... we get the real position of the row and set to TRUE
// that position in the imageStatus flags list. We also call notifyDataSetChanged
//on the adapter object to let it know that something has changed and to update!
favImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Integer realPosition = (Integer) v.getTag(); //get the position from the view's tag
imageStatus.set(realPosition, true); //this position has been clicked be the user
adapter.notifyDataSetChanged(); //notify the adapter
}
});
// set the position to the favImage as a tag, we later retrieve it
// in the onClick method
favImage.setTag(new Integer(position));
return v;
}
如果您不打算動態修改列表(刪除/添加行),這應該很好,否則您還必須照顧修改該列表imageStatus
以反映更改。你沒有說什麼是你的行數據,另一種方法(如果用戶點擊那個圖像(除了改變它),如果你打算做點什麼)的另一種方法是將圖像的狀態合併到行的數據模型中。對此這裏有一些教程:
Android ListView Advanced Interactive 或Commonsware-Android Excerpt(交互式行)
** ** getView2 ........? – waqaslam 2012-03-17 21:52:57
@Waqas:忽略這一點,那是我在課堂上覆制,粘貼和剝離的東西。它在代碼中正確命名。 – Josh 2012-03-17 21:53:41