I try to put a group of two checkboxes in each row in RecycleView. But if I click one checkbox, some other checkboxes is impacted. As the following pictures:
So are the row 25, 37 ... the following is the code
main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Root(BoxLayout):
def __init__(self, **kwargs):
super(Root, self).__init__(**kwargs)
self.rv.data = [{'value': str(x)} for x in range(100)]
class MyApp(App):
def build(self):
return Root()
if __name__ == '__main__':
MyApp().run()
my.kv:
<Row@BoxLayout>:
canvas.before:
Color:
rgba: 0.5, 0.5, 0.5, 1
Rectangle:
size: self.size
pos: self.pos
value: ''
orientation: 'horizontal'
Label:
text: root.value
size_hint:0.45, 1
CheckBox:
group: root.value
size_hint:0.05, 1
state: 'down'
CheckBox:
group: root.value
size_hint:0.05, 1
<Root>:
rv: rv
RecycleView:
id: rv
viewclass: 'Row'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
It's really simple code, but I don't know why. Could you please help?
kivy is 1.10.0
the root cause is that the viewclasses are recycled for reusing. We can derive the viewclass from RecycleDataViewBehavior(have to read the code). Implement refresh_view_attrs. Maybe add the state of checkboxes into data of recycleview. Can refer to the example of SelectableLabel in recycleview/init.py.