EndlessAdapter Updated (Now With More Endlessness!)

An eagle-eyed contributor pointed out an optimization for my EndlessAdapter, one that both simplifies reuse and helps solve a problem in cases where you might run out of data sooner than you had expected.

First, for those of you just tuning in, EndlessAdapter is a decorating Adapter, one that makes it easier for you to support data where you only want to display a subset of the possible items up front, perhaps due to network I/O considerations. When the user scrolls to the bottom, if you indicate there is more data to be had, EndlessAdapter displays a placeholder row while you, in a background thread, go fetch the additional data.

The optimization is for that placeholder row, and comes from a long-forgotten capability of the adapter framework. You can return IGNORE_ITEM_VIEW_TYPE from getItemViewType(). The getItemViewType() method is called for each row, for you to indicate what object pool the AdapterView should use for recycling for this row. Returning IGNORE_ITEM_VIEW_TYPE indicates that this row is not eligible for recycling. Ordinarily, that would be a bad idea — you want to recycle rows wherever possible. However, in this case, since the placeholder row is temporary, we have an ideal use case for IGNORE_ITEM_VIEW_TYPE. For those who have used a former edition of EndlessAdapter, you no longer have to implement rebindPendingView()!

This has a side benefit when it comes to handling problems like network failures, where you had expected to get more data, but now cannot. Just return false from cacheInBackground() and do not do anything in appendCachedData(). EndlessAdapter will simply get rid of the placeholder row.

Find out about new posts on the CommonsBlog via the Atom feed, or follow @CommonsWare on Twitter!