How to use Kivy RecycleView in KV language and with ScreenManager?

0

I'm using Kivy for an app.

I have a database in Google Firebase working fine, I can save it there quietly. I would like to return this data to my app, but before I get into trouble with it, I can not list anything in Kivy.

I wanted to use the Kivy ListView, but in the documentation it already says to use RecycleView, but I found it somewhat confusing (the documentation). It gave me doubts and could not use it.

In the RecycleView documentation 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 an 'sm', as in the example it gives itself 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 look at the syntax, 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 on one of my ScreenManager screens.

I could not find anything in the doc to help me, but I did the test codes on those two pages and they worked, but I did not know how to do it all together.

I tried the following (I imagine it was for this 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 kv document, it 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 did not have any results, it only displays the blue screen, which is the one I pull on the canvas, but it does not list anything. I have already put a print also to test, but does not appear in the terminal, then nothing is happening. This test to display 20 items is based on the documentation, which is 100 items.

From this, I want to list items from my database, but one issue at a time. I'm trying for a couple of days, sorry if it's a silly thing, but in Kivy's documentation I could not find the answer.

Thanks for your attention.

    
asked by anonymous 10.09.2018 / 22:36

1 answer

0

Just to close the question and leave it as a help if anyone needs it, I'll put here the solution I got by posting question in English.

The good feitor suggested I add the RecycleView as a child widget of my Screen. So the code, where I had my screens, looked like this:

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 the kv document:

<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 as I was able to continue to manage the screens normally.

    
13.09.2018 / 20:17