Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.recycleview import RecycleView
  4. from kivy.uix.recycleview.views import RecycleDataViewBehavior
  5. from kivy.uix.label import Label
  6. from kivy.properties import BooleanProperty
  7. from kivy.uix.recycleboxlayout import RecycleBoxLayout
  8. from kivy.uix.behaviors import FocusBehavior
  9. from kivy.uix.recycleview.layout import LayoutSelectionBehavior
  10.  
  11. Builder.load_string('''
  12. <Box>:
  13. Image:
  14. source: 'icon.png'
  15. size: self.texture_size
  16.  
  17. <SelectableLabel>:
  18. # Draw a background to indicate selection
  19. canvas.before:
  20. Color:
  21. rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
  22. Rectangle:
  23. pos: self.pos
  24. size: self.size
  25. <RV>:
  26. viewclass: 'Box'
  27. SelectableRecycleBoxLayout:
  28. default_size: None, dp(56)
  29. default_size_hint: 1, None
  30. size_hint_y: None
  31. height: self.minimum_height
  32. orientation: 'vertical'
  33. multiselect: True
  34. touch_multiselect: True
  35. ''')
  36.  
  37. class Box:
  38. pass
  39.  
  40. class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
  41. RecycleBoxLayout):
  42. ''' Adds selection and focus behaviour to the view. '''
  43.  
  44.  
  45. class SelectableLabel(RecycleDataViewBehavior, Label):
  46. ''' Add selection support to the Label '''
  47. index = None
  48. selected = BooleanProperty(False)
  49. selectable = BooleanProperty(True)
  50.  
  51. def refresh_view_attrs(self, rv, index, data):
  52. ''' Catch and handle the view changes '''
  53. self.index = index
  54. return super(SelectableLabel, self).refresh_view_attrs(
  55. rv, index, data)
  56.  
  57. def on_touch_down(self, touch):
  58. ''' Add selection on touch down '''
  59. if super(SelectableLabel, self).on_touch_down(touch):
  60. return True
  61. if self.collide_point(*touch.pos) and self.selectable:
  62. return self.parent.select_with_touch(self.index, touch)
  63.  
  64. def apply_selection(self, rv, index, is_selected):
  65. ''' Respond to the selection of items in the view. '''
  66. self.selected = is_selected
  67. if is_selected:
  68. print("selection changed to {0}".format(rv.data[index]))
  69. else:
  70. print("selection removed for {0}".format(rv.data[index]))
  71.  
  72.  
  73. class RV(RecycleView):
  74. def __init__(self, **kwargs):
  75. super(RV, self).__init__(**kwargs)
  76. self.data = [{'text': str(x)} for x in range(100)]
  77.  
  78.  
  79. class TestApp(App):
  80. def build(self):
  81. return RV()
  82.  
  83. if __name__ == '__main__':
  84. TestApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement