def create_view(self, index, viewclass=None):
"""Creates and initializes the view for the data at `index`. The
returned view is synced with the data, except for the pos/size
properties.
"""
if viewclass is None:
viewclass = self.get_viewclass(index)
if viewclass is None:
return
item = self[index]
# FIXME: we could pass the data though the constructor, but that wont
# work for kv-declared classes, and might lead the user to think it can
# work for reloading as well.
view = viewclass()
if viewclass not in _view_base_cache:
_view_base_cache[viewclass] = isinstance(view, RecycleViewMixin)
if _view_base_cache[viewclass]:
view.refresh_view_attrs(self.recycleview, item)
else:
for key, value in item.items():
setattr(view, key, value)
return view
python类properties()的实例源码
def get_views(self, i_start, i_end):
'''Gets a 2-tuple of the new and old views for the current viewport.
The new views are synced to the data except for the size/pos
properties.
The old views need to be removed from the layout, and the new views
added.
'''
current_views = self.views
visible_views = {}
new_views = []
dirty_views = self.dirty_views
get_view = self.get_view
make_view_dirty = self.make_view_dirty
# iterate though the visible view
# add them into the container if not already done
for index in range(i_start, i_end + 1):
view = get_view(index)
if not view:
continue
visible_views[index] = view
current_views.pop(index, None)
new_views.append((view, index))
# put all the hidden view as dirty views
for index, view in current_views.items():
make_view_dirty(view, index)
# save the current visible views
self.views = visible_views
return new_views, current_views.values()
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