刷新Kivy中数据更改的循环视图

2022-09-06 00:00:00 python kivy kivy-language android

更新其他类的数据时,不会更改/更新循环视图的刷新。 我已经看到关于此堆栈溢出的多个帖子,但没有一个是明确的。

每次单击‘NextScreen’按钮时,我都需要刷新循环视图。

还提供了一个清晰的示例,说明如何使用REFRESH_FROM_Data()方法以及它属于哪个类。 提前谢谢。


import kivy
import sys
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen,SlideTransition,FadeTransition  
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty,ListProperty,StringProperty
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior


kv='''
ScreenManagement:
    id:screenmgr
    
    Screen1:
        id:scr1
        name:'scr1'
        manager:screenmgr
    Screen2:
        id:scr2
        name:'scr2'
        manager:screenmgr

<Screen1>:
    GridLayout:
        cols:3
        Label:
            text: "Enter any no."
        TextInput:
            multiline:False
            id:newno
        Button:
            text: 'Submit'
            on_press: root.getinput(newno.text)
        Button:
            text:'nextscreen'
            on_press:root.refreshview()
            on_release:root.manager.current = 'scr2'

<RecycleViewlist>:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True

<Screen2>:
    BoxLayout:
        listp:rv_list
        orientation:'vertical'
        Button:
            text:'Back'
            size:150,50
            on_press:app.root.current = 'scr1'
        Recycleviewlist:
            id: rv_list

'''

mylistno = 5
class Screen1(Screen):
    #newnumber =StringProperty()
    mylistnew = 0
    def getinput(self,number):
        self.mylistnew = int(number)
    def refreshview(self):
        if self.mylistnew != 0:
            print('refreshed new',self.mylistnew)
            rvobj.createview(self.mylistnew)
class Screen2(Screen):
    pass
class Recycleviewlist(RecycleView):
    def __init__(self,**kwargs):
        super(Recycleviewlist, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(mylistno)]
    
    def createview(self,mylistnonew):
        self.data = [{'text': str(x)} for x in range(mylistnonew)]
rvobj = Recycleviewlist()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''
    
class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    #sc3 = Screen3()

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return Truej
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected


class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_string(kv)
        
class TestApp(App):
        
        def build(self):
                return presentation

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


解决方案

代码行:

rvobj = Recycleviewlist()
创建Recycleviewlist的新实例,但它不是出现在您的图形用户界面中的实例。因此,您对该实例所做的任何更改都不会影响您的图形用户界面。您需要做的是获取对您的图形用户界面中的Recycleviewlist的引用,因此尝试将refreshview()替换为:

def refreshview(self):
    # get a reference to the RecycleViewList
    rvobj = presentation.get_screen('scr2').ids.rv_list

    if self.mylistnew != 0:
        print('refreshed new', self.mylistnew)
        rvobj.createview(self.mylistnew)

并删除行:

rvobj = Recycleviewlist()

相关文章