Search code examples
pythonandroidkivykivy-languagekivymd

Kivy MDLabel in RecycleView losing text after updating data


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 this expected when using MDLabels in RecycleView?

main.py

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

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_file('main.kv')

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

main.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

Screenshot of the first enter on mainScreen: enter image description here

Screenshot of the second enter on mainScreen after data update: enter image description here


Solution

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