简体   繁体   English

如何在 kivy 中使用 RecycleView?

[英]How to use RecycleView in kivy?

Based on the code that is posted on kivy docs about Recycle View, how do I change the data?根据 kivy docs 上发布的关于 Recycle View 的代码,我该如何更改数据? how to change the size of the selectable labels?如何更改可选标签的大小? and especially if I want to have more widget on the screen, how do set the position of the list to be in the bottom side of the screen?特别是如果我想在屏幕上有更多的小部件,如何将列表的位置设置在屏幕的底部?

I've tried to change the position with GridLayout, BoxLayout and nothing happens.我尝试使用 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()

''' '''

Question 1问题 1

How do I change the data?如何更改数据?

Answer回答

Change the data by updating self.data通过更新self.data更改数据

RecycleView » data RecycleView » 数据

The RecycleView is generatad by processing the data (ie self.data ), essentially a list of dicts, and uses these dicts to generate instances of the viewclass as required. RecycleView 是通过处理数据(即self.data )生成的,本质上是一个字典列表,并根据需要使用这些字典生成视图类的实例。

Question 2问题2

How to change the size of the selectable labels?如何更改可选标签的大小?

Answer回答

The size especially the height of each selectable widgets can be change in either Python script or kv file.可以在 Python 脚本或 kv 文件中更改每个可选小部件的大小,尤其是高度。

With reference to this example, the height can be changed by setting default_size: None, dp(30) at SelectableRecycleBoxLayout .参考这个例子,可以通过在SelectableRecycleBoxLayout设置default_size: None, dp(30)来改变高度。 As for the width of each selectable widgets, it will change according to the number columns in a row of data, self.data至于每个可选择的小部件的宽度,它会根据一行数据中的列数而变化, self.data

With a SelectableRecycleGridLayout , one can specify the minimum width for each column by using cols_minimum使用SelectableRecycleGridLayout ,可以使用cols_minimum指定每列的最小宽度

Question 3问题 3

If I want to have more widget on the screen, how to set the position of the list to be in the bottom side of the screen?如果我想在屏幕上有更多的小部件,如何将列表的位置设置在屏幕的底部?

Answer回答

  • Declare a root widget with inheritance of BoxLayout声明一个继承BoxLayout的根小部件

Snippets片段

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

Output输出

按钮的 RecyclView

Example例子

How to add vertical scroll in RecycleView 如何在 RecycleView 中添加垂直滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM