def get_view(self, index):
"""Returns a view instance for the data at `index`. It looks through
the various caches and finally creates a view if it doesn't exist.
The returned view is synced with the data, except for the pos/size
properties.
"""
if index in self.views:
return self.views[index]
dirty_views = self.dirty_views
viewclass = self.get_viewclass(index)
if viewclass is None:
return
rv = self.recycleview
stale = False
view = None
if viewclass in dirty_views:
dirty_class = dirty_views[viewclass]
if index in dirty_class:
# we found ourself in the dirty list, no need to update data!
view = dirty_class.pop(index)
elif _cached_views[viewclass]:
# global cache has this class, update data
view, stale = _cached_views[viewclass].pop(), True
elif dirty_class:
# random any dirty view element - update data
view, stale = dirty_class.popitem()[1], True
elif _cached_views[viewclass]:
# global cache has this class, update data
view, stale = _cached_views[viewclass].pop(), True
if view is None:
# create a fresh one
view = self.create_view(index, viewclass)
if stale is True:
item = self[index]
if viewclass not in _view_base_cache:
_view_base_cache[viewclass] = isinstance(view,
RecycleViewMixin)
if _view_base_cache[viewclass]:
view.refresh_view_attrs(rv, item)
else:
for key, value in item.items():
setattr(view, key, value)
self.views[index] = view
return view
评论列表
文章目录