如何在Kivy中使用RecycleView?

时间:2019-04-05 14:41:23

标签: python kivy recyclerview-layout

基于关于回收视图的奇异文档上发布的代码,如何更改数据?如何更改可选标签的尺寸?尤其是如果我想在屏幕上放置更多小部件,该如何将列表的位置设置在屏幕的底部?

我尝试使用GridLayout,BoxLayout更改位置,但没有任何反应。

'''
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>:
    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
        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
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))

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


class TestApp(App):
    def build(self):
        return RV()

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

'''

1 个答案:

答案 0 :(得分:0)

问题1

  

如何更改数据?

答案

通过更新self.data

来更改数据

RecycleView » data

  

通过处理data(即   self.data),实际上是字典列表,并使用这些字典来   根据需要生成viewclass的实例。

问题2

  

如何更改可选标签的大小?

答案

可以在Python脚本或kv文件中更改大小,尤其是每个可选小部件的高度。

参考此示例,可以通过将default_size: None, dp(30)设置为SelectableRecycleBoxLayout来更改高度。至于每个可选小部件的宽度,它将根据一行数据self.data

中的列数进行更改

使用SelectableRecycleGridLayout,可以使用cols_minimum

指定每一列的最小宽度

问题3

  

如果我想在屏幕上显示更多小部件,如何设置位置   列表的底部显示在屏幕的底部?

答案

  • 声明继承为BoxLayout的根窗口小部件

摘要

<RootWidget>:
    orientation: 'vertical'
    BoxLayout:
        size_hint: 1, 0.8
    BoxLayout:
        size_hint: 1, 0.2
        RV:

输出

RecyclView of Buttons

示例

How to add vertical scroll in RecycleView