在kivy中,我可以使用选定的某个项目来初始化RecycleView,但是如果它不在屏幕上,如何使它显示选定的项目?

时间:2018-09-07 19:36:23

标签: python kivy

这里,我能够在初始化时使用select_node()动态选择一个项目,并且确实会选择它,但是它在屏幕之外的列表的一部分中,我必须滚动到它才能看到它已被选中。如何动态显示它?

class IconPicker(BoxLayout):
    popup_ref = ObjectProperty()

    def __init__(self,**kwargs):
        super (IconPicker,self).__init__(**kwargs)
        for key in reversed(sorted(md_icons)):
            self.insert(key)
        self.insert('no-selection')
        self.rv_layout.select_node(100)

main.py

from random import sample
from string import ascii_lowercase

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from settings.icon_definitions import md_icons
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty, ObjectProperty, StringProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.recycleview.layout import RecycleLayoutManagerBehavior

Builder.load_string('''
#:import md_icons settings.icon_definitions.md_icons

<SelectableIcon>:
    orientation: 'vertical'
    padding: 5
    canvas.before:
        Color:
            rgba: (0.1, 0.1, 0.1, 1) if self.selected else (0.21, 0.21, 0.21, 1)
        Rectangle:
            size: self.size
            pos: self.pos
    value: 'alarm'
    Label:
        size_hint: 1, .8
        text: u"{}".format(md_icons[root.value]) if not '' else ''
        text_size: self.size
        font_size: self.size[1] / 2
        halign: 'center'
        valign: 'middle'
        font_name: 'pics/materialdesignicons-webfont.ttf'
    Label:
        text: root.value
        size_hint: 1, .2
        text_size: self.size
        font_size: self.size[1] / 2
        halign: 'center'
        valign: 'top'

<IconPicker>:
    padding: 5
    spacing: 5
    orientation: 'vertical'
    rv: rv
    rv_layout: rv_layout

    RecycleView:
        id: rv
        rv_layout: rv_layout
        scroll_type: ['bars', 'content']
        scroll_wheel_distance: dp(114)
        bar_width: dp(10)
        viewclass: 'SelectableIcon'

        SelectableRecycleGridLayout:
            id: rv_layout
            keyboard_mode: 'managed'
            default_size: None, dp(120)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
            cols: 8
            spacing: dp(2)
            key_selection: 'selectable'
''')

class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''

    def get_nodes(self):
        nodes = self.get_selectable_nodes()
        if self.nodes_order_reversed:
            nodes = nodes[::-1]
        if not nodes:
            return None, None

        selected = self.selected_nodes
        if not selected:  # nothing selected, select the first
            self.select_node(nodes[0])
            return None, None

        if len(nodes) == 1:  # the only selectable node is selected already
            return None, None

        last = nodes.index(selected[-1])
        self.clear_selection()
        return last, nodes

    def select_next(self):
        last, nodes = self.get_nodes()
        if not nodes:
            return

        if last == len(nodes) - 1:
            self.select_node(nodes[0])
        else:
            self.select_node(nodes[last + 1])

    def select_previous(self):
        last, nodes = self.get_nodes()
        if not nodes:
            return

        if not last:
            self.select_node(nodes[-1])
        else:
            self.select_node(nodes[last - 1])

class SelectableIcon(RecycleDataViewBehavior, BoxLayout):
    ''' 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(SelectableIcon, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableIcon, 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:
            print("selection changed to {0}".format(rv.data[index]))
            #rv.parent._close_popup("{0}".format(rv.data[index]))
            #rv.rv_layout.select_next()
            #rv.rv_layout.select_node(5)

class IconPicker(BoxLayout):
    popup_ref = ObjectProperty()

    def __init__(self,**kwargs):
        super (IconPicker,self).__init__(**kwargs)
        for key in reversed(sorted(md_icons)):
            self.insert(key)
        self.insert('no-selection')
        self.rv_layout.select_node(100)

    def insert(self, value):
        self.rv.data.insert(0, {'value': value or 'default value', 'selectable': True})

    def _close_popup(self, value):
        self.popup_ref._dismiss(value)

class IconPickerPopup(Popup):
    current_sel = StringProperty('')

    def __init__(self,**kwargs):
        super (IconPickerPopup,self).__init__(**kwargs)
        self._create_popup()

    def _create_popup(self):
        icon_picker = IconPicker(popup_ref=self)
        self.add_widget(icon_picker)

    def _dismiss(self, value, *largs):
        print value
        self.dismiss()

class IconPickerApp(App):
    def build(self):
        test_popup = IconPickerPopup(current_sel='no-selection')
        test_layout = BoxLayout()
        test_button = Button()
        test_button.bind(on_release=test_popup.open)
        test_layout.add_widget(test_button)

        return test_layout

if __name__ == '__main__':
    IconPickerApp().run()

0 个答案:

没有答案