How to use Kivy Recycleview in the KV language and with Screenmanager?

Asked

Viewed 378 times

1

I’m using Kivy for an app.

I have a database in Google Firebase working well, I can save there quietly. I would like to return this data to my app, but before I have problems with it, I can’t list anything in Kivy.

I wanted to use Kivy’s Listview, but in the documentation it already says to use Recycleview, but I found it somewhat confusing (the documentation). It made me doubt and I couldn’t use.

In the documentation of Recycleview there is the following:

Builder.load_string('''
<RV>:
    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]


class TestApp(App):
    def build(self):
        return RV()

if __name__ == '__main__':
    TestApp().run()

But I use the Screenmanager to control my screens, so in the Testapp class I return a 'Sm', as in the example gives own documentation:

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

If you observe the syntax are different, and then I get lost, not knowing what to do and how to write so that the two work together and I can generate a list of items in one of my screens Screenmanager.

I could not find in the doc something to help me, but I did the test codes of these two pages and they worked, but I did not know how to do everything together.

I tried the following (In what I imagine, it was to list 20 items as Label). In my document where I control the screens (it works for all others):

class ListaEvento(Screen, RecycleView):
    def __init__(self, **kwargs):
        super(ListaEvento, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(20)] 

In my document Kv, which works well for the visual part of the other screens too:

<ListaEvento>:
    canvas:
        Rectangle:
            source: 'design/fundo.png'
            size: self.width, self.height    


    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

But I had no result, it just displays the blue screen, which is the one I pull on the canvas, but does not list anything. I even put up a print too to test, but it does not appear in the terminal, so nothing is happening. This test of displaying 20 items is based on the documentation, in which there are 100 items.

From this, I intend to list items coming from my database, but one problem at a time. I’ve been trying for a couple of days, sorry if it’s too silly, but in Kivy’s documentation I couldn’t find the answer.

Thank you for your attention.

1 answer

1


Just to close the question and leave as help in case anyone needs, I will put here the solution I was able to post to question in English.

The good feitor suggested me to add Recycleview as a child widget of my Screen. So the code, where I had my screens, stayed that way:

class ListaEventos(Screen):
    def __init__(self, **kwargs):
        super(ListaEventos, self).__init__(**kwargs)
        # assigning data in RecyclerView
        self.rv.data = [{'text': str(x)} for x in range(100)]

And in document Kv:

<ListaEventos>:
    rv: rv # expose the widget

    canvas:
        Rectangle:
            source: 'design/fundo.png'
            size: self.width, self.height

    RecycleView:
        id: rv
        viewclass: 'Label'
        RecycleBoxLayout:
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'

This was interesting for my project, because I could continue the administration of the screens normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.