【问题标题】:How to update data in RecycleView in kivy?如何在kivy中更新RecycleView中的数据?
【发布时间】:2020-02-14 10:21:12
【问题描述】:

我从 kivy 了解到,RecycleView 能够在数据更改后通过手动调用 refresh_from_data() 来更新数据。我还有几个问题。

这是我的代码。我试图简化它,因为原来的很长。

在.py中,

class FilterPage(Screen):
    def call_DB(self, price_min, price_max):
        'codes and shits'
        'Basically, I filter some result according to users input'
        'and store it in a list called real_result'
        'I want to update content and length of real_result for RecycleView'

        self.app = App.get_running_app()
        self.rv = self.app.rv
        self.rv.refresh_from_data()

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)

        global real_result
        self.data = [{'text': str(x)} for x in range(len(real_result))]

kv = Builder.load_file("test.kv")
class MyApp(App):    
    rv = RV()
    def build(self):
        return kv

MyApp().run()

在.kv中,

<ProductButton>:
    on_release: print("success")
    canvas.before:
        Color:
            rgba: (0.4, 0.9, 0.1, 0.3)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    viewclass: 'ProductButton'    
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True
        touch_multiselect: True
        spacing: 10

<ProductPage>:
    canvas:
        Rectangle:
            source: "dot01.png"
            size: root.width, root.height
            pos: self.pos
    BoxLayout:
        orientation: "vertical"
        RV:
        Button:
            text: 'Previous screen'
            size_hint: None, None
            size: 150, 50
  1. 对要更新的数据格式有什么限制吗? 我读了这个,https://kivy.org/doc/stable/_modules/kivy/uix/recycleview.html。我还应该在我的 RecycleView 类中包含这部分代码吗?
def refresh_from_data(self, *largs, **kwargs):
    self._refresh_flags['data'].append(kwargs)
    self._refresh_trigger()
  1. 我只想更新 real_result 中的内容及其长度。可以通过拨打refresh_from_data()来完成吗?调用时我应该传递任何变量吗?

  2. 我想在 self.data 中包含一些信息。怎么样?

问题有点长,我剪了很多代码。如果您需要更多详细信息,请在下面发表评论。非常感谢。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    要更改内容,您只需更改包含的数据。无需做任何额外的事情。

    class RV(RecycleView):
        def __init__(self, **kwargs):
            super(RV, self).__init__(**kwargs)
    
            global real_result
            self.data = [{'text': str(x)} for x in range(len(real_result))]
    
        def alter_data(self):
            self.data = [{'text': str(x+x)} for x in range(len(real_result))] 
    

    然后调用alter_data

    在我的代码中我使用:

    def refresh_data(self):
        self.data = []
        records_new = self.PW.dbcon.session.query ...
    
        for n_rows in records_new:
            r_tab_name = n_rows.tab_name
            d={'t_col1': r_tab_name.c1
                , 't_col2': r_tab_name.c2
                , 's_col3': r_tab_name.s3 if r_tab_name.s3 is not None else 0
                }
    
            self.data.append(d)
        self.data_for_thz=self.data
    

    千伏:

    <LocalRecyWidget>:
        BoxLayout:
            BoxLayout:
    
                Label:
                Button:
                Button:
                ...
    
    
    <MyClass>:
        BoxLayout:
            Label:
                ...
            RecycleView:
                viewclass: 'LocalRecyWidget'
                id: ....
                data: root.data_for_thz
                RecycleBoxLayout:
                    ...
    

    【讨论】:

    • 我试图在 self.rv.refresh_from_data() 之后调用 alter_data() 但失败了。我在 alter_data() 中打印了 real_result 的长度,它显示了正确的长度。但 RV 什么也没显示。我是不是在错误的地方调用了这个函数?
    • 我猜当数据发生变化时会调用 refresh_from_data() 。因此,您必须在不同的调用中更改数据。
    • 你的意思是我应该尝试更新 RecycleView 中的数据,以便我可以做一些类似于你的事情吗?
    • 当您更改数据时,我认为视图会自动刷新
    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多