[RecycleView] Print data from selected item

25 views
Skip to first unread message

Ricardo Couto

unread,
Oct 22, 2017, 8:57:00 AM10/22/17
to Kivy users support
Hello all,

Probably a very noob question: How can I print the name of the selected item in this RecycleView?

The code works (just removed all unnecessary parts) :)

python:
        self.my_list.data = [{
            'name': p.name,
            'age': p.age,}
            for p in people_list]

kv:
        RecycleView:
            id: my_list
            scroll_type: ['bars', 'content']
            scroll_wheel_distance: dp(114)
            bar_width: dp(8)
            viewclass: 'Tits'
            RecycleBoxLayout:
                default_size: None, dp(36)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                spacing: dp(2)

Thanks!

Kevin Diaz

unread,
Oct 22, 2017, 8:35:12 PM10/22/17
to kivy-...@googlegroups.com
Hello,

This was the solution I implemented in a program I wrote earlier in the year. I believe this was straight from the documentation too.

You also need to add/replace this to your KV : 
<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0.5, 0.2, 0.1, 1) if self.selected else (0.5, 0.5, 0.5, 1)
        Rectangle:
            pos: self.pos
            size: self.size

# This is within whatever the main class is not within the above class, basically just
# adjust some of these properties in your current declaration you have below.
      RecycleView:
            id: rv
            scroll_type: ['bars', 'content']
            scroll_wheel_distance: dp(114)
            bar_width: dp(10)
            viewclass: 'SelectableLabel'
            SelectableRecycleBoxLayout:
                default_size: None, dp(56)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                multiselect: False
                touch_multiselect: False





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. """
       
self.selected = is_selected
       
if is_selected:
           
print("selection changed to {0}".format(rv.data[index]))
Reply all
Reply to author
Forward
0 new messages