kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.26k stars 675 forks source link

Kivy MDLabel in RecycleView losing text after updating data #1515

Open domdrag opened 1 year ago

domdrag commented 1 year ago

Description of the Bug

I'm having two screens, loginScreen and mainScreen. In the mainScreen I have a RecycleView of MDLabels. Initially when entering on mainScreen everything works fine, but whenever I'm refreshing the data of my RecycleView, the text of some labels keeps disappearing and appearing on scrolling. When I use regular kivy labels instead of MDLabels, I'm not getting this strange behavior. Am I doing something wrong in the code or is it a bug?

Code and Logs

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen
from kivymd.color_definitions import colors
from kivymd.uix.boxlayout import MDBoxLayout
import random

kv = """
<LoginScreen>:
    name: 'loginScreen'

    Button:
        text: 'MAIN'
        on_release: root.switchButton()

<DailyService>:
    bg_color: app.theme_cls.primary_dark
    day: ''
    service: ''

    MDGridLayout:
        rows: 2
        MDLabel:
            halign: 'center'
            text: root.day

        MDLabel:
            halign: 'center'
            md_bg_color: root.bg_color
            text: root.service

<MainScreen>:
    name: 'mainScreen'
    rvid: myRv

    MDRecycleView:
        viewclass: 'DailyService'
        id: myRv
        RecycleBoxLayout:
            default_size: None, dp(200)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'

    Button:
        pos_hint:{"x":0.5,'bottom': 1}
        size_hint: 0.4, 0.1
        text: 'LOGIN'
        on_release: root.switchButton()

MyScreenManager:
    loginScreen: loginScreenId
    mainScreen: mainScreenId

    LoginScreen:
        id: loginScreenId

    MainScreen:
        id: mainScreenId
"""

class DailyService(MDBoxLayout):
    pass

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToLoginScreen()

class LoginScreen(Screen):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToMainScreen()

class MyScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        #self.current = 'loginScreen'

    def switchToMainScreen(self):
        data = []
        for i in range(20):
            k = random.randint(0, 9)
            if k%2 == 0:
                color =  colors['BlueGray']['700']
            else:
                color =  colors['Green']['700']
            data.append({'day': 'DAY',
                         'service': 'SERVICE',
                         'bg_color':  color})
        self.mainScreen.rvid.data = data
        self.current = 'mainScreen'

    def switchToLoginScreen(self):
        self.current = 'loginScreen'

class MyApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = 'Dark'
        self.theme_cls.primary_palette = 'Blue'
        self.theme_cls.accent_palette = 'Amber'
        return Builder.load_string(kv)

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

Screenshots

Good: Screenshot of the first enter on mainScreen (good) Bad (after update): Screenshot of the second enter on mainScreen after data update (bad)

Versions

domdrag commented 1 year ago

The issue appears to be solved when using Kivy and Kivymd from the master branch.