Kivy RecycleView可以替代ListView吗?它是如何工作的? [英] Kivy RecycleView as an alternative to ListView? How does it work?

查看:233
本文介绍了Kivy RecycleView可以替代ListView吗?它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I should preface that I'm still a newbie to Kivy. I tried looking for similar questions, but they were either outdated or unclear.

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

I was looking for something to display a list of elements among which a used could select one to interact with other widgets (buttons and so on). I stumbled upon the documentation page on ListView, but it clearly states that ListView is deprecated and RecycleView must be used instead.

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

Now the problem is that the documentation doesn't seem very clear (at least to me) about how to use RecycleView. It's definitely more complicated than other widgets and I can't seem to figure it out.

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

To break this down into more digestible questions: 1. How do I define a RecycleView that acts as a list of items? 2. How do I provide the items for it? 3. How do I interact with it, specifically about making only one item selectable at a time, detecting when something is selected and automatically making something selected on event?

哦,我想尽可能使用kv语言.

Oh by the way, I prefer using kv language whenever possible.

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

I would much appreciate some help with finding or understanding documentation resources that would allow me to understand this more generally for future usage. I really wish there was a tutorial for these complicated features somewhere but if it exists, it's damn hard to find it.

推荐答案

下面的示例说明如何使用Recycleview显示按钮列表,并在选择每个按钮时显示一个弹出窗口.

The example below illustrates how to use Recycleview to display a list of buttons and when each button is selected, it display a popup window.

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()

test.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"

输出

这篇关于Kivy RecycleView可以替代ListView吗?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆