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

Refresh Recycle view on data changes in Kivy(刷新Kivy中数据更改的循环视图)
本文介绍了刷新Kivy中数据更改的循环视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

每次单击‘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()

这篇关于刷新Kivy中数据更改的循环视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)