好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

kivy recyleview 实现任意数字选择

from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.recycleview import RecycleViewfrom kivy.uix.recycleview.views import RecycleDataViewBehaviorfrom kivy.uix.label import Labelfrom kivy.properties import BooleanPropertyfrom kivy.uix.recycleboxlayout import RecycleBoxLayoutfrom kivy.uix.behaviors import FocusBehaviorfrom kivy.uix.recycleview.layout import LayoutSelectionBehavior

Builder.load_string(''':
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (1, 0, 1, .5) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        canvas:
            Color:
                rgba: (1, 1, 0,1)
            Rectangle:
                pos: self.pos
                size: self.size
        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
''')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 False
        if self.collide_point(*touch.pos) and self.selectable:
            try:
                for one_color in self.parent.children:
                    if one_color.selected:
                        one_color.selected = False
                    if self.parent.parent.data[self.index]["text"]==one_color.text:
                        self.parent.parent.data_selected[0]=one_color.text
                        one_color.selected=True
            except:
                passclass RV(RecycleView):
    def __init__(self,data_range=range(2000,2100),**kwargs):
        super(RV, self).__init__(**kwargs)
        self.data_selected=[0]
        self.data = [{'text': str(x)} for x in data_range]class TestApp(App):
    def build(self):
        return RV()if __name__ == '__main__':
    TestApp().run()

               

查看更多关于kivy recyleview 实现任意数字选择的详细内容...

  阅读:31次