如何从Kivy RecycleView重设所选项目?

时间:2019-04-11 03:30:44

标签: python kivy

我有一个基本的Kivy RecycleView,它使我可以从队列中多选不同的选项。我有一些代码,它获取与队列中每个项目相关的ID并进行一些工作(即,打印出一个Excel文件)。我希望能够在搜索下一个列表之前将选定的项目列表重新设置为空列表。

我有两个问题:

  1. 我能够将该项目添加到我的选定项目列表中(并在未选中时将其删除),但是一旦我添加了resetList()方法,我的“选定项目”列表就保持为空。
  2. 我的第二个问题是,当我重置所选项目列表时,UI仍然选中了这些项目(即突出显示)。然后,我如何浏览列表并从python端取消选中每个SelectableLabel?

我最终希望能够使用一些搜索条件来获取事件日期列表,然后将其显示在recycleview中。然后,用户将在列表中选择要为其打印excel文档的每个事件,然后单击一个类似“将所选事件打印到excel”的按钮。所以这篇文章是针对我试图重置列表的问题的。我最初试图将重置绑定在“搜索”按钮上,该按钮使用搜索条件建立了recycleview。

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)
    selectedList = ["default"]


    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. '''
        self.selected = is_selected
        if is_selected:
            self.selectedList.append(rv.data[index])
            print("Adding: " + str(rv.data[index]))
        else:
            try:
                self.selectedList.remove(rv.data[index])
                print("Removing: " + str(rv.data[index]))
            except:
                print("error removing data from list. Couldn't find: " + str(rv.data[index]) + " in the list.")

    def getList(self):
        return self.selectedList

    def resetList(self):
        print("resetting")
        self.selectedList = []


class RV(RecycleView):

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = []
        print("RV Init")

class GuiApp(App):
    theme_cls = ThemeManager()
    theme_cls.theme_style = 'Dark'
    previous_date = ''
    previous_date2 = ''
    StartDate = ObjectProperty("Start Date")
    EndDate = ObjectProperty("End Date")
    theLabel = SelectableLabel()

    def update(self, rv):
        print("Updating....")
        rv.data = []
        #self.theLabel.resetList()
        for event in self.serviceList:
            print(event)
            rv.data.append({'text': event})

    def goReset(self):
        self.theLabel.resetList()

此页面完全没有格式化,我只是在粗略地研究功能。


Kivy File

<AnotherScreen>:
    id: AnotherScreen
    name: "AnotherScreen"
    rows: 5
    padding: 10
    spacing: 10
    canvas.before:
        Color:
            rgba: .8, .8, .8, .25
        Rectangle:
            pos: self.pos
            size: self.size

    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Harvest Intel'
                with_previous: False
            ActionButton:
                text: 'Home'
                on_release: root.manager.current = "MainScreen"
                on_press: app.on_home_select(rv)
                on_release: root.ids.mySpinner.text = "Ministry"
            ActionButton:
                text: 'Settings'
    BoxLayout:
        FloatLayout:
            Label:
                text: "Choose a Service Type or Date Range: "
                pos_hint: {'center_x': .25 , 'center_y': .8}

            Label:
                text: "Service Type: "
            Spinner:
                id: mySpinner
                size_hint:(None,None)
                size: root.width/4,root.height/12
                text: "Ministry"                   #default value showed
                values: ["Higher Ground","Awana"]       #list of values to show
                pos_hint: {'center_x': 0.55, 'center_y': .75}
                size_hint: (.28,.08)
                on_text: app.chooseServiceType(self.text)

            FloatLayout:
                orientation: 'vertical'

                MDRaisedButton:
                    id: StartDate
                    text: app.StartDate
                    size_hint: None, None
                    size: 3 * dp(48), dp(48)
                    pos_hint: {'center_x': .5, 'center_y': .5}
                    opposite_colors: True
                    on_release: app.show_example_date_picker(1)
                MDLabel:
                    id: date_picker_label
                    text: ''
                    theme_text_color: 'Primary'
                    size_hint: None, None
                    size: dp(48)*3, dp(48)
                    pos_hint: {'center_x': .5, 'center_y': .5}

                MDRaisedButton:
                    id: EndDate
                    text: app.EndDate
                    size_hint: None, None
                    size: 3 * dp(48), dp(48)
                    pos_hint: {'center_x': .5, 'center_y': .25}
                    opposite_colors: True
                    on_release: app.show_example_date_picker(2)
                MDLabel:
                    id: date_picker_label
                    text: ''
                    theme_text_color: 'Primary'
                    size_hint: None, None
                    size: dp(48)*3, dp(48)
                    pos_hint: {'center_x': .5, 'center_y': .5}
                Button:
                    id: getdates
                    text: "getdates"
                    size_hint: .5,.1
                    on_press: app.getFutureServices(rv)


                Button:
                    text: "print list"
                    pos_hint: {'center_x': 0.2, 'center_y': .4}
                    size_hint: .3, .3
                    on_press: app.goReset()
                    on_press: app.printList()



        BoxLayout:
            orientation: 'vertical'
            Label:
                size_hint: (1,.3)
                text: 'Services'
            RV:
                id: rv
                viewclass: 'SelectableLabel'
                SelectableRecycleBoxLayout:
                    default_size: None, dp(56)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True


<SelectableLabel>:
    # Draw a background to indicate selection

    canvas.before:
        Color:
            rgba: (.0, 0.6, .1, .5) if self.selected else (.2, .2, .2, 1)
        Rectangle:
            pos: self.pos
            size: self.size

我希望可以在代码中使用所选项目的完整列表,但是一旦它们单击“搜索”按钮,该列表就会被清除,并且RecycleView中的所有选定行都未被选中,但是我我不确定该如何更改。

存储所选列表的最佳方法是什么,然后又可以将UI中的所选项目列表和所选标签重置为开始的方式。

0 个答案:

没有答案