2012-07-31 04:56:29 +00:00
|
|
|
from kivy.adapters.listadapter import ListAdapter
|
2012-07-31 15:00:04 +00:00
|
|
|
from kivy.uix.listview import ListItemButton, ListItemLabel, \
|
|
|
|
CompositeListItem, ListView
|
2012-07-31 04:56:29 +00:00
|
|
|
from kivy.uix.gridlayout import GridLayout
|
|
|
|
|
2012-08-05 17:41:06 +00:00
|
|
|
from datastore_integers import datastore_integers
|
2012-07-31 04:56:29 +00:00
|
|
|
|
2012-07-31 05:03:08 +00:00
|
|
|
class MainView(GridLayout):
|
2012-07-31 04:56:29 +00:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
kwargs['cols'] = 2
|
|
|
|
kwargs['size_hint'] = (1.0, 1.0)
|
2012-07-31 05:03:08 +00:00
|
|
|
super(MainView, self).__init__(**kwargs)
|
2012-07-31 04:56:29 +00:00
|
|
|
|
2012-08-01 17:53:43 +00:00
|
|
|
# This is quite an involved args_converter, so we should go
|
|
|
|
# through the details. x here is a data item object, be it
|
|
|
|
# a string for a typical usage, as here, or some other object.
|
|
|
|
# x will become the text value when the class used in this
|
|
|
|
# example, CompositeListItem, is instantiated with the args
|
|
|
|
# returned by this converter. All of the rest, for size_hint_y,
|
|
|
|
# height, and the cls_dicts list, will be passed in the call
|
|
|
|
# to instantiate CompositeListItem for a data item. Inside the
|
|
|
|
# constructor of CompositeListItem is special-handling code that
|
|
|
|
# uses cls_dicts to create, in turn, the component items in the
|
|
|
|
# composite. This is a similar approach to using a kv template,
|
|
|
|
# which you might wish to explore also.
|
2012-07-31 15:00:04 +00:00
|
|
|
args_converter = \
|
2012-08-01 17:53:43 +00:00
|
|
|
lambda x: \
|
|
|
|
{'text': x,
|
|
|
|
'size_hint_y': None,
|
|
|
|
'height': 25,
|
|
|
|
'cls_dicts': [{'cls': ListItemButton,
|
|
|
|
'kwargs': {'text': "Left",
|
|
|
|
'merge_text': True,
|
|
|
|
'delimiter': '-'}},
|
|
|
|
{'cls': ListItemLabel,
|
|
|
|
'kwargs': {'text': "Middle",
|
|
|
|
'merge_text': True,
|
|
|
|
'delimiter': '-',
|
|
|
|
'is_representing_cls': True}},
|
|
|
|
{'cls': ListItemButton,
|
|
|
|
'kwargs': {'text': "Right",
|
|
|
|
'merge_text': True,
|
|
|
|
'delimiter': '-'}}]}
|
|
|
|
|
2012-07-31 04:56:29 +00:00
|
|
|
item_strings = ["{0}".format(index) for index in xrange(100)]
|
2012-08-01 17:53:43 +00:00
|
|
|
|
|
|
|
# And now the list adapter, constructed with the item_strings as
|
2012-08-05 17:41:06 +00:00
|
|
|
# the data, a datastore to add the required is_selected boolean onto
|
|
|
|
# data records, and our args_converter() that will operate one each
|
2012-08-01 17:53:43 +00:00
|
|
|
# item in the data to produce list item view instances from the
|
|
|
|
# :class:`CompositeListItem` class.
|
2012-07-31 04:56:29 +00:00
|
|
|
list_adapter = ListAdapter(data=item_strings,
|
2012-08-05 17:41:06 +00:00
|
|
|
datastore=datastore_integers,
|
2012-07-31 15:00:04 +00:00
|
|
|
args_converter=args_converter,
|
2012-07-31 04:56:29 +00:00
|
|
|
selection_mode='single',
|
|
|
|
allow_empty_selection=False,
|
|
|
|
cls=CompositeListItem)
|
2012-08-01 17:53:43 +00:00
|
|
|
|
|
|
|
# Use the adapter in our ListView:
|
2012-07-31 04:56:29 +00:00
|
|
|
list_view = ListView(adapter=list_adapter)
|
|
|
|
|
|
|
|
self.add_widget(list_view)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from kivy.base import runTouchApp
|
2012-07-31 15:00:04 +00:00
|
|
|
runTouchApp(MainView(width=800))
|