我只想写一个聊天室应用程序,想在 ListView 中实现一个消息列表,所以 - 消息概念
但我不知道如何做到这一点,因为 kivy 上的列表适配器只能接受列表的 ListItemButton 或 ListItemLabel 元素。他不接受我的自定义类的类型,甚至不接受简单的 BoxLayout。提前谢谢你。
首先你不再使用ListView,因为它已被弃用,kivy推荐使用RecycleView,概念类似于ListView,有一个数据字段,即字典列表,其中字典是使用视图的widget的属性接收创建每个项目。
假设自定义小部件已经拥有它,那么您必须公开列表可以修改的所有属性,在示例中有 3 个属性:source_image、title 和 message。
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
Builder.load_string('''
<CustomWidget@BoxLayout>:
source_image: ""
title: ""
message: ""
orientation: 'horizontal'
Image:
source: root.source_image
size_hint_x: 1
BoxLayout:
size_hint_x: 2
orientation: 'vertical'
Label:
text: root.title
size: self.texture_size
size_hint_y: None
Label:
text: root.message
<View>:
viewclass: 'CustomWidget'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
''')
class View(RecycleView):
def __init__(self, **kwargs):
super(View, self).__init__(**kwargs)
self.data = [{'source_image': "heart.png", 'title': "title{}".format(i), 'message': "message"} for i in range(100)]
if __name__ == '__main__':
runTouchApp(View())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句