Search code examples
pythonkivykivy-language

Class inheritance with Kivy's RecycleView


I was trying to create a generic RecycleView and inherit from it changing the viewclass, like this:

<GenericList@RecycleView>:
    SelectableRecycleBoxLayout:
        canvas.before:
            Color:
                rgba: (.0, 0.9, .5, .8)
            Rectangle:
                pos: self.pos
                size: self.size
        default_size: None, 30
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: False

<SetList@GenericList>:
    viewclass: 'SetSelectableLabel'
<CardList@GenericList>:
    viewclass: 'CardSelectableLabel'

The difference between SetSelectableLabel and CardSelectableLabel is just the method being called with the event on_selected. I noticed that when running an app with this configuration, the RecycleView wouldn't show any of the viewclass objects. The list is there, I can scroll it and see the scroll bar moving, but none of the labels are shown.

But if I define the viewclass in the RecycleView like below, everything works, except that I can't do what I want, which is change the viewclass on inherited classes for custom behavior.

<GenericList@RecycleView>:
    viewclass: 'GenericSelectableLabel'

Am I missing something here?


Solution

  • RecycleView is an incomplete widget. There are many things you can't. For example:

    rv = RecycleView()
    rv.add_widget(RecycleBoxLayout())
    

    the code above doesn't work. You must construct a widget tree from kv like this:

    RecycleView:
        RecycleBoxLayout:
    

    viewclass is one of those. You can't change it dynamically, so the code below:

    rv = Bulider.load_string('''
    RecycleView:
        RecycleBoxLayout:
    ''')
    rv.viewclass = 'Label'  # doesn't work
    

    doesn't work. I'm not sure this is the cause of the issue, but you just need to be aware of that kind of incompleteness.