Что касается ListView, используйте ModalView для управления выбором и запускайте метод getname () .Пожалуйста, обратитесь к двум примерам (ListView и RecycleView) для получения подробной информации.
Примечание
ListView устарело с версии 1.10.0, используйте RecycleView вместо.
Пример 1 - ListView
mainlistview.py
from kivy.app import App
from kivy.uix.modalview import ModalView
from kivy.properties import ObjectProperty
class MyListView(ModalView):
list_view = ObjectProperty(None)
def __init__(self, **kwargs):
super(MyListView, self).__init__(**kwargs)
self.list_view.adapter.bind(on_selection_change=self.callback_function)
def callback_function(self, instance):
print(instance)
App.get_running_app().getname()
class MainListViewApp(App):
title = "ListView ListItemButton Demo"
def build(self):
return MyListView()
def getname(self):
print("App.getname() - Called")
if __name__ == '__main__':
MainListViewApp().run()
mainlistview.kv
#:kivy 1.10.0
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter
<MyListView>:
list_view: list_view
ListView:
id: list_view
adapter:
la.ListAdapter(
data=["Item #{0}".format(i) for i in range(100)],
selection_mode='single',
allow_empty_selection=True,
cls=lv.ListItemButton)
Вывод - ListView
Пример 2 - RecycleView
mainrecycleview.py
from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.properties import ObjectProperty
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
""" Adds selection and focus behaviour to the view. """
pass
class SelectableButton(RecycleDataViewBehavior, Button):
""" Add selection support to the Label """
index = None
def refresh_view_attrs(self, rv, index, data):
""" Catch and handle the view changes """
self.index = index
return super(SelectableButton, self).refresh_view_attrs(rv, index, data)
def on_release(self):
print("Pressed & released on", self.text)
App.get_running_app().getname()
class RV(RecycleView):
rv_layout = ObjectProperty(None)
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': "Button " + str(x), 'id': str(x)} for x in range(100)]
class MainRecycleViewApp(App):
title = "RecycleView Button Demo"
def build(self):
return RV()
def getname(self):
print("RecycleView: App.getname() - Called")
if __name__ == "__main__":
MainRecycleViewApp().run()
mainrecycleview.kv
#:kivy 1.10.0
<SelectableButton>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (0.0, 0.9, 0.1, 0.3)
Rectangle:
pos: self.pos
size: self.size
<RV>:
rv_layout: layout
viewclass: 'SelectableButton'
SelectableRecycleBoxLayout:
id: layout
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
Вывод - RecycleView