English 字幕

← ListView and Recycling - Developing Android Apps


Showing Revision 15 created 05/24/2016 by Udacity Robot.

  1. The ListView starts by requesting a view
    for every visible item,
  2. however many you can fit onto the screen.
  3. It'll also create a couple in either
    direction to make sure we can scroll
  4. without seeing a flicker
    as a new view is created and populated.
  5. Then it creates new items just in time.
  6. So it's next in line
    to be visible to the user.
  7. So if the user never scrolls
    to the bottom of the list,
  8. the ListView will never request
    that view from the adapter.
  9. But this is really just a half measure.
  10. As you can see,
    if the user keeps scrolling,
  11. we could potentially
    just keep adding new views,
  12. even if they disappear
    off the top of the screen.
  13. Eventually that's going to lead
    to the same impact in memory use
  14. and performance as if we had
    just created all of these views
  15. directly at the beginning.
  16. The solution is recycling each view
    as it scrolls off the screen,
  17. allowing it to be reused
    when we need to show another item
  18. as it moves into view at the top or bottom.
  19. So rather than having to create
    and then hold in memory
  20. each item of the list
    as it comes into view,
  21. we only need to do the creation step
    for the number of visible items
  22. and a couple on either side.
  23. Then, whenever a new list item
    comes into view,
  24. we just update the data displayed
    in one of our items in our recycle bin.
  25. The result?
  26. Less memory overhead, smoother scrolling
    and less view management
  27. you have to do yourself.
  28. This same recycling behavior is implemented
  29. across all AdapterView descended classes,
    such as GridView and ListView,
  30. which also introduces the reason
    that the adapter isn't built
  31. directly into these controls themself.
  32. By keeping them separate,
  33. your adapter defines how to display
    each element of the underlying data,
  34. while the adapter view
    implementation itself
  35. is responsible for controlling
    how each of these elements is laid out.
  36. Be it a list or a grid
    in these particular instances.