Android Kivy widgets的创建真的很慢还是我做错了?

Android Kivy widgets的创建真的很慢还是我做错了?,android,python,kivy,Android,Python,Kivy,我注意到我的应用程序中的小部件创建很慢,所以我创建了这个简单的测试应用程序来检查它。它包含两个屏幕,每个屏幕都包含小部件的简单列表 应用程序: list.kv文件: <ListScreen>: items_box: items_box BoxLayout: orientation: "vertical" AnchorLayout: size_hint_y: 0.1 padding: sel

我注意到我的应用程序中的小部件创建很慢,所以我创建了这个简单的测试应用程序来检查它。它包含两个屏幕,每个屏幕都包含小部件的简单列表

应用程序:

list.kv文件:

<ListScreen>:
    items_box: items_box
    BoxLayout:
        orientation: "vertical"
        AnchorLayout:
            size_hint_y: 0.1
            padding: self.width*0.1, self.height*0.05
            Label:
                font_size: root.height*0.05
                text: "Some list"
        ScrollView:
            size_hint_y: 0.9
            size: self.size
            BoxLayout:
                id: items_box
                orientation: "vertical"
                padding: self.width*0.1, 0
                size_hint_y: None

<ListItem>:
    orientation: "horizontal"
    size_hint_y: None
    height: app.sm.height*0.1
    Label:
        font_size: app.sm.height*0.025
        text: root.title
        size_hint_x: 0.9
        text_size: self.size
        valign: "middle"
    CheckBox
        size_hint_x: 0.1
<ListScreen>:
    recycle_view: recycle_view
    items_box: items_box
    BoxLayout:
        orientation: "vertical"
        AnchorLayout:
            size_hint_y: 0.1
            padding: self.width*0.1, self.height*0.05
            Label:
                font_size: root.height*0.05
                text: "Some list"
        RecycleView:
            id: recycle_view
            size_hint: 1, 0.9
            viewclass: "ListItem"
            RecycleBoxLayout:
                id: items_box
                orientation: "vertical"
                padding: self.width*0.1, 0
                default_size_hint: 1, None
                size_hint: 1, None
                height: self.minimum_height

<ListItem@BoxLayout>:
    orientation: "horizontal"
    size_hint: 1, None
    height: app.sm.height*0.1
    title: ''
    Label:
        font_size: app.sm.height*0.025
        text: root.title
        size_hint_x: 0.9
        text_size: self.size
        valign: "middle"
    CheckBox
        size_hint_x: 0.1

来吧,真的吗?创建包含50行标签和复选框的简单列表平均需要0.5秒?还是我做错了什么?

多亏了Alexander Taylor(Kivy开发者之一)才找到了解决方案。以下是他的答案:

小部件创建相对较慢,特别是取决于 小部件包含

如果像在你的例子中那样列出大的清单,你通常会过得更好 使用RecycleView。这将优化事物,以重用较少数量的资源 滚动期间的小部件

所以我试过了

应用程序:

list.kv文件:

<ListScreen>:
    items_box: items_box
    BoxLayout:
        orientation: "vertical"
        AnchorLayout:
            size_hint_y: 0.1
            padding: self.width*0.1, self.height*0.05
            Label:
                font_size: root.height*0.05
                text: "Some list"
        ScrollView:
            size_hint_y: 0.9
            size: self.size
            BoxLayout:
                id: items_box
                orientation: "vertical"
                padding: self.width*0.1, 0
                size_hint_y: None

<ListItem>:
    orientation: "horizontal"
    size_hint_y: None
    height: app.sm.height*0.1
    Label:
        font_size: app.sm.height*0.025
        text: root.title
        size_hint_x: 0.9
        text_size: self.size
        valign: "middle"
    CheckBox
        size_hint_x: 0.1
<ListScreen>:
    recycle_view: recycle_view
    items_box: items_box
    BoxLayout:
        orientation: "vertical"
        AnchorLayout:
            size_hint_y: 0.1
            padding: self.width*0.1, self.height*0.05
            Label:
                font_size: root.height*0.05
                text: "Some list"
        RecycleView:
            id: recycle_view
            size_hint: 1, 0.9
            viewclass: "ListItem"
            RecycleBoxLayout:
                id: items_box
                orientation: "vertical"
                padding: self.width*0.1, 0
                default_size_hint: 1, None
                size_hint: 1, None
                height: self.minimum_height

<ListItem@BoxLayout>:
    orientation: "horizontal"
    size_hint: 1, None
    height: app.sm.height*0.1
    title: ''
    Label:
        font_size: app.sm.height*0.025
        text: root.title
        size_hint_x: 0.9
        text_size: self.size
        valign: "middle"
    CheckBox
        size_hint_x: 0.1

快100多倍!这真是太棒了。

投了赞成票,但我真的不喜欢RecycleView当视图在RecycleView中占据很大的空间时,只有第一个项目是在初始化时创建的,另一个是在您滚动到它们时创建的,这对于您的用户来说是不好的app@SPSP而且,RecycleView似乎无法正确处理大小取决于内容的项目(例如标签的高度:self.texture\u size[1])。
<ListScreen>:
    recycle_view: recycle_view
    items_box: items_box
    BoxLayout:
        orientation: "vertical"
        AnchorLayout:
            size_hint_y: 0.1
            padding: self.width*0.1, self.height*0.05
            Label:
                font_size: root.height*0.05
                text: "Some list"
        RecycleView:
            id: recycle_view
            size_hint: 1, 0.9
            viewclass: "ListItem"
            RecycleBoxLayout:
                id: items_box
                orientation: "vertical"
                padding: self.width*0.1, 0
                default_size_hint: 1, None
                size_hint: 1, None
                height: self.minimum_height

<ListItem@BoxLayout>:
    orientation: "horizontal"
    size_hint: 1, None
    height: app.sm.height*0.1
    title: ''
    Label:
        font_size: app.sm.height*0.025
        text: root.title
        size_hint_x: 0.9
        text_size: self.size
        valign: "middle"
    CheckBox
        size_hint_x: 0.1
11-29 13:11:58.196 13121 13203 I python  : 0.00388479232788
11-29 13:11:59.192 13121 13203 I python  : 0.00648307800293
11-29 13:12:00.189 13121 13203 I python  : 0.00288391113281
11-29 13:12:01.189 13121 13203 I python  : 0.00324606895447
11-29 13:12:02.194 13121 13203 I python  : 0.00873804092407
11-29 13:12:03.188 13121 13203 I python  : 0.00265002250671
11-29 13:12:04.209 13121 13203 I python  : 0.00614500045776