2015-01-14 20 views
1
我在與Android的ListView一個奇怪的問題

&的GridViewAndroid的 - ListView的,爲什麼list.getChildAt(2).findViewById(R.id.icon)返回multipes

我有一個充有一個ListView 100個項目,該XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/main_screen"> 
    <RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/icon_1" 
     android:onClick="changeIcon" /> 
    </RelativeLayout> 
</RelativeLayout> 

public void changeIcon(View view) { 
    ImageView myIcon= (ImageView)view.findViewById(R.id.icon); 
    myIcon.setImageResource(R.drawable.icon_2); 
} 

如果我點擊它取代我感動(這是我想要什麼)的圖標列表中的第一個圖標,但是,它也取代了超出的圖標查看,不是所有的,但它發生在一個序列..

因此,在圖片中,我的觸摸選擇了1個,13,25,37等..被自動選中。當我快速向後滾動時,我在#1上的圖標隨機變化。有時去#2,或#3 ..

同樣的問題上發生的GridView ..

任何人都可以解釋這種現象,我能做些什麼來避免這種..

鏈接截圖: http://imgur.com/a/5pFMA

圖像1是空列表, 圖像2是Me點擊圖標#1, 圖像3是也13上#1改變通過我的感人節目,

回答

0

問題是ListView一旦滾動就重新使用視圖。

您應該獨立保存每個列表項的狀態。這應該在自定義適配器中完成。

+0

嗨,謝謝你的回覆..關於如何保存項目狀態的任何提示? – trancer

+0

我明白了,謝謝你的提示:) – trancer