gpt4 book ai didi

python-3.x - Kivy RecycleView 作为 ListView 的替代品?它是如何工作的?

转载 作者:行者123 更新时间:2023-12-04 15:18:23 25 4
gpt4 key购买 nike

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

我正在寻找一些东西来显示一个元素列表,其中一个用户可以选择一个与其他小部件(按钮等)交互。
我偶然发现了 documentation page on ListView ,但它明确指出不推荐使用 ListView 和 RecycleView必须改用。

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

把它分解成更容易理解的问题:
1. 如何定义一个作为项目列表的 RecycleView?
2. 我如何为其提供物品?
3. 我如何与它交互,特别是关于一次只能选择一个项目,检测何时选择某物并在事件时自动选择某物?

哦,顺便说一句,我更喜欢尽可能使用 kv 语言。

我非常感谢在查找或理解文档资源方面的帮助,这将使我能够更广泛地理解这一点以备将来使用。我真的希望在某处有一个关于这些复杂功能的教程,但如果它存在,那么很难找到它。

最佳答案

下面的例子说明了如何使用 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()

测试.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"

输出

Recycleview of buttons with popups

关于python-3.x - Kivy RecycleView 作为 ListView 的替代品?它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com