【发布时间】:2018-02-09 21:49:37
【问题描述】:
我正在尝试创建一个简单的考勤应用程序。
当程序启动时,所有标签都在取消选择列表中
预期行为:当任何标签被选中时,数据会移动到选中的列表中,现在选中的标签位于连接列表的末尾。然后 RecycleView 刷新以显示此更改。
所以我设法让数据从一个列表移动到另一个列表,但我无法让 RecycleView 刷新
我尝试使用 id 但失败了
我希望有人可以帮助我。我认为这对于知识渊博的人来说是一个常规问题,但对于像我这样的菜鸟来说却不是。
我是第一次在这个网站上提问,所以提前抱歉
代码如下:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from datetime import datetime
import kivy
from kivy.config import Config
Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '500')
importedlist = ['Novella Varela', 'Caroll Faircloth', 'Douglas Schissler',
'Rolande Hassell', 'Hayley Rivero', 'Niesha Dungy', 'Winfred Dejonge', 'Venetta Milum']
deselected_list = importedlist[:]
selected_list = []
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view.
and add/remove items from lists
'''
self.selected = is_selected
if self.selected and self.text in deselected_list:
selected_list.append(self.text)
deselected_list.remove(self.text)
print(selected_list)
elif not self.selected and self.text in selected_list:
deselected_list.append(self.text)
selected_list.remove(self.text)
print(deselected_list)
class RV(RecycleView):
# this needs to be updated every time any label is selected or deselected
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = ([{'text': str(row)} for row in sorted(deselected_list)]
+ [{'text': str(row)} for row in sorted(selected_list)])
class Screen(BoxLayout):
now = datetime.now()
def nowdate(self):
return self.now.strftime('%d')
def nowmonth(self):
return self.now.strftime('%m')
def nowyear(self):
return self.now.strftime('%y')
def nowhour(self):
return self.now.strftime('%H')
def nowminute(self):
return self.now.strftime('%M')
Builder.load_string('''
#:import datetime datetime
<Screen>:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 30
TextInput:
id: date
text: root.nowdate()
TextInput:
id: date
text: root.nowmonth()
TextInput:
id: date
text: root.nowyear()
TextInput:
id: date
text: root.nowhour()
TextInput:
id: date
text: root.nowminute()
Button:
RV:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(45)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: True
touch_multiselect: True
Button:
size_hint_y: None
height: 30
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
''')
class TestApp(App):
def build(self):
return Screen()
if __name__ == '__main__':
TestApp().run()
【问题讨论】:
-
'预期的行为:当任何标签被选中时,数据移动到选中的列表,现在选中的标签位于连接列表的末尾。然后 RecycleView 刷新以显示此更改。您的意思是要在将标签添加到新列表时删除它们吗?因此,如果我单击“Carrol Faircloth”,她的名字会从 RecycleViewLayout 中删除吗?所以我们然后在房车上看到除了她以外的其他人?
-
非常感谢您的回复!我的意思是“Carrol Faircloth”在这个列表的底部。如果我想改变主意并取消选择她,她将留在底部。所有选定的标签都将位于列表的底部,它们实际上将在一个单独的排序列表中(取消选择和选定的列表将在另一个之下,并且每次我选择或取消选择任何内容时它们都会排序)。我仍然尝试想办法做到这一点,但主要问题是我正在使用 RecycleView 并且我不明白如何将每个列表项与这种刷新行为绑定
-
为了进一步澄清,我们有两个列表,两个回收视图(每个列表一个),然后是这些列表中的一堆标签?在我写一个例子之前先确定一下。
-
使用 ids 我也尝试将其添加到 SelectableLabel: on_selected: self.ids.RV_id.refresh_from_layout() 并且我仍然收到错误使用 self.parent 没有帮助,因为 SelectableLabel 父级是 RecycleBoxLayout (不是 RecycleView)
-
是的。我不认为它们是两个独立的 RecycleView,但它们可能是)