开发者

Problems while setting the ImageView source in the ItemLayout of a ListView

i have a problem while setting the source of an ImageView that is inside an Item of a ListView. The problem is as follows:

In my XML LayoutFile for the ListItems i have an ImageView and a TextView:

<?xml version="1.0" encoding="utf-8"开发者_开发百科?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:paddingLeft="10dp"
android:orientation="horizontal">

<ImageView android:id="@+id/actionState"
    android:layout_width="12dp"
    android:layout_height="12dp"
    android:layout_gravity="center_vertical"
    android:src="@drawable/check_lightgrey"
    android:layout_marginRight="5dp"/>

<TextView android:id="@+id/actionText"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    />

</LinearLayout>

In my res/drawable folder i have the two files check_lightgrey.png and check_green.png. Drawing the ListView with an ArrayAdapter in that way that every ListItem consists of the check_lightgrey.png followed by the coresponding text works fine.

When the user clicks on a ListItem i want change the source from the ImageView of this ListItem from check_lightgrey.png to check_green.png

The Code of my setOnItemClickListener in my ListActivity looks like this:

ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageView imageView = (ImageView) v.findViewById(R.id.actionState);
            imageView.setImageResource(R.drawable.check_green);
        }
    });

This works fine as long as the List is not so full that scrolling is needed. If there are that many ListItems that you need to scroll, then there is a bug when clicking on a ListItem: Not only the source of the ImageView in the clicked ListItem is set but also the source of the ImageViews of ListItems that are not visible because they are out of the current view.

It sounds a little confusing, but i will give an example:

  • There are 56 items in the Listview
  • without scrolling you can see the first 13 Items
  • now click on Item 1 -> the source of the ImageView changes as aspected
  • now scrolling down the source of the ImageView of second Item (Item 15) that was not visible before has also changed
  • the same thing is with Item 29 and with Item 43
  • as you can see there is always a difference of 14

The same effect exists when changing the oriantation of the device to landscape. The Only thing that changes is the difference that is 7.

I searched for a conclusion but didn't find anything. So it woud be nice if anybody could help me or give me a hint.

greetings


First, it's not a bug. It happens because ListView is trying to recycle views in order to be faster. So, in order to change the image of one of the rows you must change the data that is behind the list, update your adapter and call notifyDataSetChanged.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜