0

I have just came across an issue that is getting me mad. I have a viewpager with several Fragments (all of them are instances of the same class) which hold a listview. Each listview row has a view which I load in a task asynchronously. When the task finishes it sets the bitmap into the imageView. There is no point in showing the code as I am sure it is correct. At the beginning I started using ListFragment for simplicity and everything worked fine. The listviews scroll smoothly and the async task are executed for the views inside the same fragment as the user scrolls down. But then I realized I need to customize the listview, so I created my own layout and set it to the ListFragment as the android developer docs states (using a predefined id for the listview so the ListFragment can find it) overriding onCreateView(). And here comes the strange thing: Everything goes fine but NOW when the user scrolls and the asynctasks finish and sets the bitmap the viewpager recreates the whole views of the selected fragment and the adjacent one which causes more asynctask to be executed and the cycle repeats everytime an asynctask sets a bitmap. This causes a lot of freezing when the user scrolls due to the massive redraw and asynctask calls. I have also tried to create a custom Fragment (not inheriting from ListFragment) with a listview but it happens the same, the viewpager recreates unnecesary views everytime a thread sets a bitmap into a row. Someone please can explain me WHY this doesnt happen when I use ListFragment and it does happen in the other case? How can I solve it? Thanks in advance.

EDIT:
I found the issue. Surprisingly it is in the listview xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/white" >
    </ListView>

</RelativeLayout>

If I set the listview property android:layout_height="wrap_content" instead of match_parent then it recreates the views of the adjacent pages and itself unnecesary everytime a bitmap is set asynchronously. The problem now is that I cannot set the height to wrap_content thus even if a listview has a few items it is drawn till the bottom which is ugly. Any suggestions please?

Quark
  • 402
  • 4
  • 15

1 Answers1

0

ok I finally found the answer in this post Android: wrap_content is not working with ListView

It should never be used wrap_contentwith a listView, otherwise getView()will be called many times unnecesary. Seriously, this should be somewhere explained in the docs, it made me waste a lot of time.

Community
  • 1
  • 1
Quark
  • 402
  • 4
  • 15