Android Endless list memory management

后端 未结 9 2077
温柔的废话
温柔的废话 2021-02-05 07:42

I\'m implementing an endless listview by loading more items to the arraylist in the onScrollStateChanged(...) method. If I\'m implementing this scheme for fetching more than 1 m

相关标签:
9条回答
  • 2021-02-05 08:07

    You should use paging and Load More button as a footer of ListView. For example:

    url = http://your_full_url.php?page=1

    let say you have 100 records in each page then very first time get all these 100 records of page 1, display those on ListView and cache them. Now scroll down your ListView and click on Load more button (Load More button should be set as footer of the ListView).

    When you click on Load More you will get next 100 records by calling

    url = http://your_full_url.php?page=2 and so on for

    url = http://your_full_url.php?page=3,

    url = http://your_full_url.php?page=4 etc...

    each time you will cache those records so that in case of connection loss you could show records available in cache.

    0 讨论(0)
  • 2021-02-05 08:07

    If you need to keep in memory 1M objects, and assuming the object data is small, than theis is just a few MB of memory, and should be fine to just keep in memory. From the question I understand that you will read more items when users scrolls forward, so in practice you will not have 1M rows - users will need to scroll for a long time to get to 1M. As long as you use the ListView properly, you can have the adapter data grow to be 1M+ rows in memory without any issue

    0 讨论(0)
  • 2021-02-05 08:09

    In Android the ListView is virtualized. Practically that means that there's no actual limit for number of elements inside it. You can put millions of rows inside the list, it'll only allocate memory for the currently visible ones (or a few more tops).

    Source

    Also check this article Performance Tips for Android’s ListView

    0 讨论(0)
  • 2021-02-05 08:14

    If your ListView contains only text items, there is not much you need to do. However, if you are loading more memory intense things, like drawables (for example, you have a picture on the right side of your view), then you should do some recycling, for best result. You might receive an OutOfMemoryException very quickly on a weaker device. I could go OOM even on a Nexus 4. Just try to scroll very quickly, up and down, up and down, and repeat until force close.

    Take a look at RecyclerListener, it is very easy to implement.

    0 讨论(0)
  • 2021-02-05 08:14

    Tianwei's approach is the way to go.

    If the ListView is lazy loaded and since the ListView itself is recycling views it's best to keep only the visible list entries in memory. You basically do the same in the adapter the ListView does for the Views.

    If you'd keep all data in memory what would be the point of lazy loading the ListView anyway? Just load all data and skip the lazy loading part... Of course with the lazy loading approach that does load only the visible data (and maybe some more) you'd have to implement lazy loading at the bottom and the top of the list to make this work.

    Now since there's no information on the nature of the data (text, images) or the source (Internet, SQLite Db, text file...) I can't give you code (samples) how to implement this. If you elaborate on the data I can answer the question more accurately.

    0 讨论(0)
  • 2021-02-05 08:21

    I guess sqlite Database and streaming parser(GSON).

    0 讨论(0)
提交回复
热议问题