【问题标题】:Replacing kivy Listview with RecycleView用 RecycleView 替换 kivy Listview
【发布时间】:2019-06-01 10:13:55
【问题描述】:

我目前正在尝试用 RecycleView 替换使用 ListView 的应用程序中的某些功能。

从文档中我无法弄清楚如何做到这一点。

当前的代码类似于这样:

ListView:
    id: x
    adapter:
        sla.SimpleListAdapter(data=[], cls=label.Label)


x.adapter.data.append(‘frank’)

是否有任何资源或提示来说明如何实现这一目标? 我正在尝试使用 recycleview,因为 ListView 现在似乎已被弃用。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    ListView 在 Kivy 版本 1.11.0 中被删除。下面的 sn-ps 显示了 RecycleView 中的等价物。 Kivy 文档中有两个RecycleView 的示例。

    下面的 sn-ps 显示了使用 RecycleView 的等效项。

    片段:kv 文件

    RecycleView:
        id: x
    
        viewclass: 'Label'
    
        RecycleBoxLayout:
            default_size: None, dp(26)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
    

    片段:py 文件

    x.data.append({'text‘: 'frank’)
    

    示例

    以下三个示例说明了 Kivy RecycleView

    main.py

    ​​>
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.recycleview import RecycleView
    from kivy.uix.recycleview.views import RecycleDataViewBehavior
    from kivy.uix.label import Label
    from kivy.properties import BooleanProperty
    from kivy.uix.recycleboxlayout import RecycleBoxLayout
    from kivy.uix.behaviors import FocusBehavior
    from kivy.uix.recycleview.layout import LayoutSelectionBehavior
    
    Builder.load_string('''
    <SelectableLabel>:
        # Draw a background to indicate selection
        canvas.before:
            Color:
                rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
            Rectangle:
                pos: self.pos
                size: self.size
    
    <RV>:
        id: rv
    
        # Optional: ScrollView attributes
        bar_width: 5
        bar_color: 1, 0, 0, 1   # red
        bar_inactive_color: 0, 0, 1, 1   # blue
        effect_cls: "ScrollEffect"
        scroll_type: ['bars', 'content']
    
        viewclass: 'SelectableLabel'
    
        SelectableRecycleBoxLayout:
            default_size: None, dp(26)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
            multiselect: True
            touch_multiselect: True
    ''')
    
    
    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)
    
        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 True
            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 RV(RecycleView):
        def __init__(self, **kwargs):
            super(RV, self).__init__(**kwargs)
            self.data = [{'text': str(x)} for x in range(100)]
            self.data.insert(0, {'text': 'frank'})
            self.data.append({'text': 'liam'})
    
    
    class TestApp(App):
        title = 'Kivy RecycleView Demo'
    
        def build(self):
            return RV()
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2017-10-28
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多