快速解决方案
我相信最简单的方法是遍历回收框中的元素,如果选择了它们,则取消选择,并将文本设置为
""
. 考虑在
TestView
班级:
def remove_selection(self):
# Remove text
self.parent.children[1].text = ""
# Remove selection
for child in self.children[0].children:
if child.selected:
child.selected = False
当然,您必须在按下按钮时调用它,您可以将布局更改为:
Button:
id: reset_btn
on_press: test_view.remove_selection()
稍微好一点的方法
打给孩子和家长的电话可能会在更复杂的布局中变得相当混乱,所以我想建议一种更干净的方法来解决您的问题。由于一次只处理一个选定的项,我认为最好的方法是将项本身存储在程序中,并在需要时从中检索信息。准确地说,不是储存
name_selected
和
text_selected
在你的
test_view
,您只需存储所选项目。为此,必须引入一些更改:
-
进口
ObjectProperty
和
StringProperty
:
from kivy.properties import BooleanProperty, ObjectProperty, StringProperty
-
增加一个
selected
归因于
测试视图
类,允许
None
有助于检查是否选择了任何内容:
class TestView(RecycleView):
selected = ObjectProperty(None, allownone=True)
-
编辑
remove_selection
我上面提到的方法:
def remove_selection(self):
# Remove selection
if self.selected is not None:
self.selected.selected = False
self.selected = None
-
添加
选定的文本
和
已选择姓名
属性到
SelectableLabel
班级:
class SelectableLabel(RecycleDataViewBehavior, Label):
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
text_selected = StringProperty()
name_selected = StringProperty()
-
编辑
apply_selection
方法:
def apply_selection(self, rv, index, is_selected):
self.selected = is_selected
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
# Store selected text and name inside the label
self.name_selected = rv.data[index]['text']
self.text_selected = rv.data[index]['description']
# Remember the selection
rv.selected = self
else:
print("selection removed for {0}".format(rv.data[index]))
-
更改显示在
text_lbl
:
Label:
id: text_lbl
text: "" if test_view.selected is None else test_view.selected.text_selected
-
删除不需要的属性(
选定的文本
,
已选择姓名
从
<TestView>
:
<TestView>:
viewclass: 'SelectableLabel'