简体   繁体   中英

How can I use the RecycleView of Kivy on kv language with ScreenManager?

I have a database on Firebase of Google working well, I can save my data there easily. I would like to return this data for my app, but before I have problems with this, I can't list anything on Kivy.

I would want to use the ListView of Kivy, but in the documentation is recommended to use the RecycleView. But I can't understand the documentation. I have some doubts.

If you can read the docs of RecycleView , you'll see this as an example:

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'm using the ScreenManager to control my screens, then, in the TestApp class I return 'sm', like this example of the 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 see the syntaxes are different, and it's here where I don't know how to code this. I would like to keep using the ScreenManager to control the screens and use a RecycleView to return my data in a list.

How can I use the RecycleView with my ScreenManager? This is my main.py, I configure the screen in another document, and I use the ki language too. So if you all can to do an example to me I will be grateful.

import kivy
from kivy.app import App, Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager
from telas.telas import Acesso, Comprando, Vendendo, CadastrarEvento


kivy.require('1.10.1')
Builder.load_file('ing.kv')
Config.read('config.ini')
sm = ScreenManager()

sm.add_widget(Acesso(name='acesso'))
sm.add_widget(Comprando(name='comprando'))
sm.add_widget(Vendendo(name='vendendo'))
sm.add_widget(CadastrarEvento(name='cadastrarEvento'))
sm.add_widget(ListaEventos(name='listaEventos'))

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

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

Here the kv that I tried the first time

<ListaEventos>:
    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'

ListaEventos:

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

You should not inherit from 2 widgets but what widget is going to be painted? For example, if you want an image that behaves like a button, you must inherit from the Image widget and the ButtonBehavior class, that is, visually it is an image but added the button behavior.

So to solve your problem it is not correct to use the inheritance but the composition, that is, to add the RecyclerView as a son of the Screen.

*.py

import kivy
from kivy.app import App, Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen

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)]


kivy.require('1.10.1')
Builder.load_file('ing.kv')
Config.read('config.ini')

sm = ScreenManager()
sm.add_widget(ListaEventos(name='listaEventos'))


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


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

ing.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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM