【问题标题】:Kivy recycleview trouble displaying complex widgetsKivy recycleview 显示复杂小部件时出现问题
【发布时间】:2021-07-14 06:48:33
【问题描述】:

我在将复杂的小部件添加到 recycleview 时遇到问题,主要是尺寸问题。 Recycleview 似乎对每一行中的内容都有大小限制。

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.popup import Popup
from kivymd.uix.card import MDCard
    
Builder.load_string('''
#:kivy 1.10.0
    
<RecycleViewRow>:
    size_hint_y: None
    size: "180dp", "280dp"
    pos_hint: {"center_x": .5, "center_y": .5}
    orientation: "vertical"
    padding: 10
    border_radius: 20
    radius: [15]
    elevation:0
    MDLabel:
        text: root.text
        theme_text_color: "Custom"
        font_style: "H6"
        font_size: "20sp"
        text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
                            
    MDBoxLayout:
        orientation: "vertical"
        padding: 20, 0
        MDBoxLayout:
            MDLabel:
                text: "State: "
                theme_text_color: "Custom"
                font_style: "Subtitle2"
                font_size: "14sp"
                text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
        MDLabel:
            text: "state name"
            theme_text_color: "Custom"
            font_style: "Body2"
            font_size: "12sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)   
    MDBoxLayout:
        MDLabel:
            text: "City: "
            theme_text_color: "Custom"
            font_style: "Subtitle2"
            font_size: "14sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
        MDLabel:
            text: "city name"
            theme_text_color: "Custom"
            font_style: "Body2"
            font_size: "12sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)   
    MDBoxLayout:
        MDLabel:
            text: "Locality: "
            theme_text_color: "Custom"
            font_style: "Subtitle2"
            font_size: "14sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
        MDLabel:
            text: "locality name"
            theme_text_color: "Custom"
            font_style: "Body2"
            font_size: "12sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
    MDBoxLayout:
        MDLabel:
            text: "Pincode: "
            theme_text_color: "Custom"
            font_style: "Subtitle2"
            font_size: "14sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
        MDLabel:
            text: "pincode"
            theme_text_color: "Custom"
            font_style: "Body2"
            font_size: "12sp"
            text_color: (0/255.0,141/255.0,155/255.0,255/255.0)   
    MDBoxLayout:
        MDLabel:
            text: "Last Verified: "
            theme_text_color: "Error"
            font_style: "Subtitle2"  
        MDLabel:
            text: "verification time"
            theme_text_color: "Error"
            font_style: "Subtitle2"
            font_size: "14sp"
    MDRaisedButton:
        text: "Get Details"
        md_bg_color: 0/255.0,141/255.0,155/255.0,255/255.0
    
<MainScreen>:
    viewclass: 'RecycleViewRow'
    RecycleGridLayout:
        cols:1
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'                    
''')
    
    
class RecycleViewRow(MDCard):
    text = StringProperty()   
    
class MainScreen(RecycleView):    
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.data = [{'text': "Button " + str(x), 'id': str(x)} for x in range(100)]
    

    
class TestApp(MDApp):
    title = "RecycleView Direct Test"
    
    def build(self):
        return MainScreen()
    
if __name__ == "__main__":
    TestApp().run()

This is how it looks

This is how it should look

我已经用普通的滚动视图完成了这项工作,但是生成所有框及其内容需要很长时间,因为我需要生成其中的许多。

就像我在下面所说的那样,我想出了这部分,但知道我无法将它与中心对齐。就像如果我减小宽度,我需要这样做,它会留在左边。我什至尝试将recycleview 放到anchorlayout 中,但没有得到锚定。

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    很抱歉,您错误地使用了回收视图。回收行为有 4 个组成部分。

    1. 回收视图
    2. 循环布局
    3. 视图类
    4. 数据。

    RecycleView 是基本小部件。 RecycleView 内部是 RecycleLayout。它决定了 RecycleChildrenWidgets 的布局。事实上,RecycleChildrenWidgets 并不作为您创建的小部件存在,而是作为字典存在。这些字典存储在 RecycleView.data 列表中。字典包含小部件属性的值。小部件类型在 viewclass' 属性下的 RecycleView 中设置。
    因此,您需要创建一个自定义小部件,其属性类似于 data 列表中字典的 {attribute:value}。
    按照链接中的示例进行操作。 https://kivy.org/doc/stable/api-kivy.uix.recycleview.html

    如果您不想走那条路,可以改用 ScrollView 小部件。

    【讨论】:

    • 哦,好的,谢谢。那么我应该为此使用哪个视图类?该文档并未完全显示我对此的选择。因为这不适合标签下。我正在使用滚动视图,但我正在加载许多小部件,因此加载时间太长。还有其他方法可以快速加载多个小部件吗?
    • @andril 您可以创建自己的类并将其名称作为字符串传递给 viewclass。您想在小部件中定义的任何属性都可以作为 id 放在 root 中。
    【解决方案2】:

    我发现了问题所在。我不得不更改 default_size 值。老实说,recycleview 的文档需要一些工作,尽管我想我应该仔细阅读它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多