【问题标题】:Stopping RecycleView from sorting items停止 RecycleView 对项目进行排序
【发布时间】:2021-06-14 07:03:15
【问题描述】:

在我的程序的一部分中,我有一个使用 RecycleBoxLayout 的 RecycleView,它们一起拥有一堆 DownloadItem 实例,这些实例本身继承自 MDCard强>。

recycleview 的问题在于它以一种荒谬的方式对项目进行排序。我希望项目按照添加的顺序显示,而不是按回收视图排序。

示例: RecycleView sorting items

我的 Python 文件:

from kivy.lang import Builder
from kivy.properties import StringProperty, ColorProperty
from kivy.animation import Animation
from kivymd.uix.card import MDCard
from kivymd.app import MDApp


class DownloadItem(MDCard):
    path = StringProperty()
    url_type = StringProperty("file")

    def __init__(self, **kwargs):
        self.paused = False
        self.fill_animation = None
        super(DownloadItem, self).__init__(**kwargs)

    def pause_resume_download(self):
        if not self.paused:
            self.ids.pause_resume_button.icon = "play"
            if self.fill_animation is not None:
                self.fill_animation.cancel(self.ids.progress_bar)
            self.paused = True
        else:
            self.ids.pause_resume_button.icon = "pause"
            self.fill_animation = Animation(value=100, duration=4)
            self.fill_animation.bind(on_complete=lambda *args: Animation(color=self.theme_cls.accent_color,
                                                                            duration=Example.color_duration)
                                        .start(self.ids.progress_bar))
            self.fill_animation.start(self.ids.progress_bar)
            self.paused = False
        Animation(color=app.pause_color if self.paused else self.theme_cls.primary_color,
                    duration=Example.color_duration).start(self.ids.progress_bar)

    def cancel_download(self):
        if self.fill_animation is not None:
            self.fill_animation.cancel(self.ids.progress_bar)
        Animation(color=app.fail_color,
                duration=Example.color_duration).start(self.ids.progress_bar)

class Example(MDApp):
    fail_color = ColorProperty([255 / 255, 99 / 255, 71 / 255, 1.0])
    pause_color = ColorProperty([240 / 255, 163 / 255, 10 / 255, 1.0])
    success_color = ColorProperty(None)
    color_duration = .15


    def __init__(self, **kwargs):
        global app
        super(Example, self).__init__(**kwargs)
        self.kv = Builder.load_file("design.kv")
        self.path = "C:/Users/Family/Downloads/"
        app = self

    def build(self):
        self.theme_cls.theme_style = "Dark"
        return self.kv

    def add_item(self):
        self.kv.ids.downloads_list.data.append({"path": self.path + '/' if self.path[-1] != '/' else self.path,
                                            "url_type": "file"})


if __name__ == '__main__':
    Example().run()

我的 KV 文件:

#:kivy 2.0.0
<TooltipMDLabel@MDLabel+MDTooltip>
<DownloadItem>:
    orientation: "vertical"
    padding: 10
    spacing: 10
    size_hint_y: None
    height: 100
    elevation: 20
    border_radius: 5
    radius: [5]
    MDBoxLayout:
        adaptive_height: True
        spacing: 5
        MDIcon:
            icon: root.url_type
            size_hint_x: None
            width: self.texture_size[0]
        TooltipMDLabel:
            text: root.path if len(root.path) <= 30 else root.path[:31] + " ..."
            tooltip_text: f"Path: {root.path}\nType: {root.url_type}"
            tooltip_bg_color: app.theme_cls.bg_darkest
            tooltip_text_color: app.theme_cls.opposite_bg_darkest
            size_hint_y: None
            height: self.texture_size[1]
    MDSeparator:
    MDBoxLayout:
        spacing: 10
        MDProgressBar:
            id: progress_bar
            min: 0
            max: 100
            value: 50
            color: app.theme_cls.primary_color
        MDIconButton:
            id: pause_resume_button
            icon: "pause"
            pos_hint: {"center_x": .5, "center_y": .5}
            on_release: root.pause_resume_download()
        MDIconButton:
            icon: "close"
            pos_hint: {"center_x": .5, "center_y": .5}
            on_release: root.cancel_download()
BoxLayout:
    orientation: "vertical"
    spacing: 10
    RecycleView:
        id: downloads_list
        viewclass: "DownloadItem"
        RecycleBoxLayout:
            default_size: None, 100
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: "vertical"
            padding: 15
            spacing: 15
    MDRaisedButton:
        text: "Add"
        size_hint_x: 1
        on_release: app.add_item()

【问题讨论】:

    标签: python python-3.x kivy kivymd


    【解决方案1】:

    RecycleView 不对项目进行排序。它们按照它们在data 中出现的顺序显示。您看到的是“回收”行为,您的 DownloadItems 被回收,看起来它们已排序。尝试在每个 DownloadItem 的显示中添加一个计数,您会更清楚地看到发生了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多