Here is an example of how to create a Recycleview with custom widgets. The comments in the code describe how the elements work together.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty
kv = '''
<TwoButtons>:
# This class is used as the viewclass in the RecycleView
# The means this widget will be instanced to view one element of data from the data list.
# The RecycleView data list is a list of dictionaries. The keys in the dictionary specify the
# attributes of the widget.
Button:
text: root.left_text
on_release: print(f'Button {self.text} pressed')
Button:
text: root.right_text
on_release: print(f'Button {self.text} pressed')
BoxLayout:
orientation: 'vertical'
Button:
size_hint_y: None
height: 48
text: 'Add widget to RV list'
on_release: rv.add()
RV: # A Reycleview
id: rv
viewclass: 'TwoButtons' # The view class is TwoButtons, defined above.
data: self.rv_data_list # the data is a list of dicts defined below in the RV class.
scroll_type: ['bars', 'content']
bar_width: 10
RecycleBoxLayout:
# This layout is used to hold the Recycle widgets
default_size: None, dp(48) # This sets the height of the BoxLayout that holds a TwoButtons instance.
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height # To scroll you need to set the layout height.
orientation: 'vertical'
'''
class TwoButtons(BoxLayout): # The viewclass definitions, and property definitions.
left_text = StringProperty()
right_text = StringProperty()
class RV(RecycleView):
rv_data_list = ListProperty() # A list property is used to hold the data for the recycleview, see the kv code
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.rv_data_list = [{'left_text': f'Left {i}', 'right_text': f'Right {i}'} for i in range(2)]
# This list comprehension is used to create the data list for this simple example.
# The data created looks like:
# [{'left_text': 'Left 0', 'right_text': 'Right 0'}, {'left_text': 'Left 1', 'right_text': 'Right 1'},
# {'left_text': 'Left 2', 'right_text': 'Right 2'}, {'left_text': 'Left 3'},...
# notice the keys in the dictionary correspond to the kivy properties in the TwoButtons class.
# The data needs to be in this kind of list of dictionary formats. The RecycleView instances the
# widgets, and populates them with data from this list.
def add(self):
l = len(self.rv_data_list)
self.rv_data_list.extend([{'left_text': f'Added Left {i}', 'right_text': f'Added Right {i}'} for i in range(l,l+1)])
class RVTwoApp(App):
def build(self):
return Builder.load_string(kv)
RVTwoApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/f0cd7a52-8fd5-40af-9e58-033dbe1eedabn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62267c12.1c69fb81.47125.85abSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
You can have widgets of different types in one RecycleView using the key_viewclass attribute.
Under you kivy install you can find the kivy-examples directory (if you installed it) located at… \share\kivy-examples\widgets\recycleview\key_viewclass.py
There are a number of RV examples. Below is the example using the key_viewclass.
'''
A form generator, using random data, but can be data driven (json or whatever)
Shows that you can use the key_viewclass attribute of RecycleView to select a
different Widget for each item.
'''
from random import choice, choices
from string import ascii_lowercase
from kivy.app import App
from kivy.lang import Builder
from kivy import properties as P
KV = r'''
<RVTextInput,RVCheckBox,RVSpinner>:
size_hint_y: None
height: self.minimum_height
index: None
title: ''
<RVTextInput@BoxLayout>:
value: ''
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
TextInput:
text: root.value
on_text: app.handle_update(self.text, root.index)
size_hint_y: None
height: dp(40)
multiline: False
<RVCheckBox@BoxLayout>:
value: False
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
CheckBox:
active: root.value
on_active: app.handle_update(self.active, root.index)
size_hint_y: None
height: dp(40)
<RVSpinner@BoxLayout>:
value: ''
values: []
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
Spinner:
text: root.value
values: root.values
size_hint_y: None
height: dp(40)
on_text: app.handle_update(self.text, root.index)
FloatLayout:
RecycleView:
id: rv
data: app.data
key_viewclass: 'widget'
size_hint_x: 1
RecycleBoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
'''
class Application(App):
'''A form manager demonstrating the power of RecycleView's key_viewclass
property.
'''
data = P.ListProperty()
def build(self):
root = Builder.load_string(KV)
rv = root.ids.rv
self.data = [
self.create_random_input(rv, index)
for index in range(20)
]
return root
def handle_update(self, value, index):
if None not in (index, value):
self.data[index]['value'] = value
def create_random_input(self, rv, index):
return choice((
self.create_textinput,
self.create_checkbox,
self.create_spinner
))(rv, index)
def create_spinner(self, rv, index):
"""
create a dict of data for a spinner
"""
return {
'index': index,
'widget': 'RVSpinner',
'value': '',
'values': [
letter * 5
for letter in ascii_lowercase[:5]
],
'ready': True,
}
def create_checkbox(self, rv, index):
"""
create a dict of data for a checkbox
"""
return {
'index': index,
'widget': 'RVCheckBox',
'value': choice((True, False)),
'title': ''.join(choices(ascii_lowercase, k=10)),
'ready': True,
}
def create_textinput(self, rv, index):
"""
create a dict of data for a textinput
"""
return {
'index': index,
'widget': 'RVTextInput',
'value': ''.join(choices(ascii_lowercase, k=10)),
'title': ''.join(choices(ascii_lowercase, k=10)),
'ready': True,
}
if __name__ == "__main__":
Application().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALyvCi_LmC6SCO_Lr03spvo%2BHXeuHN%3DLawQiJ75whjtow9erwA%40mail.gmail.com.