on_release 绑定到 KivyMD 中的 OneLineIconListItem

Binding of on_release to OneLineIconListItem in KivyMD

我正在尝试修改 https://kivymd.readthedocs.io/en/latest/themes/icon-definitions/ 中的 KivyMD OneLineIconListItem 示例,该示例执行我一直试图为当前项目找出的自动完成搜索。

TODO:将 on_release 添加到项目以打印项目并添加到列表

是我发现最接近我想做的事情,但我很难适应它。

这是我目前的情况:

from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from kivymd.icon_definitions import md_icons
from kivymd.uix.list import OneLineIconListItem

Builder.load_string(
'''
#:import images_path kivymd.images_path


<CustomOneLineIconListItem>
    on_release: root.print_item

    IconLeftWidget:
        icon: root.icon


<PreviousMDIcons>

    MDBoxLayout:
        orientation: 'vertical'
        spacing: dp(10)
        padding: dp(20)

        MDBoxLayout:
            adaptive_height: True

            MDIconButton:
                icon: 'magnify'

            MDTextField:
                id: search_field
                hint_text: 'Search icon'
                on_text: root.set_list_md_icons(self.text, True)

        RecycleView:
            id: rv
            key_viewclass: 'viewclass'
            key_size: 'height'

            RecycleBoxLayout:
                padding: dp(10)
                default_size: None, dp(48)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
'''
)


class CustomOneLineIconListItem(OneLineIconListItem):
    def print_item(self, instance):
        print(instance, instance.text)
    icon = StringProperty()


class PreviousMDIcons(Screen):

    def set_list_md_icons(self, text="", search=False):
        """Builds a list of icons for the screen MDIcons."""

        def add_icon_item(name_icon):
            self.ids.rv.data.append(
                {
                    "viewclass": "CustomOneLineIconListItem",
                    "icon": name_icon,
                    "text": name_icon,
                    "callback": lambda x: x,
                }
            )

        self.ids.rv.data = []
        for name_icon in md_icons.keys():
            if search:
                if text in name_icon:
                    add_icon_item(name_icon)
            else:
                add_icon_item(name_icon)


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = PreviousMDIcons()

    def build(self):
        return self.screen

    def on_start(self):
        self.screen.set_list_md_icons()



MainApp().run()

您只需更换

<CustomOneLineIconListItem>
    on_release: root.print_item

<CustomOneLineIconListItem>
    on_release: root.print_item(self) # Call the method by passing this instance.