Kivy RecycleView可以替代ListView吗?它是如何工作的?

Fed*_*o S 6 python-3.x kivy kivy-language

我应该以我仍然是Kivy的新手开头。我试图寻找类似的问题,但它们要么过时,要么不清楚。

我一直在寻找一种显示元素列表的方法,在这些元素中,一个使用过的元素可以选择一个元素以与其他小部件(按钮等)进行交互。我偶然发现了ListView文档页面,但明确指出不赞成使用ListView,而必须使用RecycleView

现在的问题是(至少对我来说)文档关于如何使用RecycleView不太清楚。它肯定比其他小部件复杂,我似乎无法弄清楚。

要将其分解为更易消化的问题:1.如何定义充当项目列表的RecycleView?2.我该如何提供物品?3.我如何与之交互,特别是一次只能选择一项,检测何时选择了某项,并在事件发生时自动进行了某项选择?

哦,对了,我更喜欢尽可能使用kv语言。

在查找或理解文档资源方面,我将不胜感激,这些资源将使我能够更广泛地理解此信息,以备将来使用。我真的希望有一个针对这些复杂功能的教程,但是如果它存在的话,那真是很难找到它。

iko*_*lim 6

下面的例子说明了如何使用 Recycleview 来显示按钮列表,当每个按钮被选中时,它会显示一个弹出窗口。

例子

主文件

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.properties import ListProperty, StringProperty, ObjectProperty


class MessageBox(Popup):

    def popup_dismiss(self):
        self.dismiss()


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    """ Adds selection and focus behaviour to the view. """
    selected_value = StringProperty('')
    btn_info = ListProperty(['Button 0 Text', 'Button 1 Text', 'Button 2 Text'])


class SelectableButton(RecycleDataViewBehavior, Button):
    """ Add selection support to the Label """
    index = None

    def refresh_view_attrs(self, rv, index, data):
        """ Catch and handle the view changes """
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_press(self):
        self.parent.selected_value = 'Selected: {}'.format(self.parent.btn_info[int(self.id)])

    def on_release(self):
        MessageBox().open()


class RV(RecycleView):
    rv_layout = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': "Button " + str(x), 'id': str(x)} for x in range(3)]


class TestApp(App):
    title = "RecycleView Button Popup Demo"

    def build(self):
        return RV()


if __name__ == "__main__":
    TestApp().run()
Run Code Online (Sandbox Code Playgroud)

测试.kv

#:kivy 1.10.0

<MessageBox>:
    title: 'Popup Message Box'
    size_hint: None, None
    size: 400, 400

    BoxLayout:
        orientation: 'vertical'
        Label:
            text: app.root.rv_layout.selected_value
        Button:
            size_hint: 1, 0.2
            text: 'OK'
            on_press:
                root.dismiss()

<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0.0, 0.9, 0.1, 0.3)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    rv_layout: layout
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        id: layout
        default_size: None, dp(56)
        default_size_hint: 0.1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: "vertical"
Run Code Online (Sandbox Code Playgroud)

输出

带有弹出窗口的按钮的回收视图

  • 我很欣赏你的回答。我显然仍然缺乏对一些基本概念的理解,无法完全掌握代码的每一部分,但我会彻底研究它。谢谢。 (2认同)