1

I am currently trying to replace some features in an app using the ListView with the RecycleView.

From the documentation I can't figure out how to that though.

The current code looks similar to this:

ListView:
    id: x
    adapter:
        sla.SimpleListAdapter(data=[], cls=label.Label)


x.adapter.data.append(‘frank’)

Are there any resources or tips on how to achieve this? I am trying to use the recycleview because the ListView seems to be deprecated now.

Druckermann
  • 681
  • 2
  • 11
  • 31

1 Answers1

0

ListView is removed in Kivy version 1.11.0. Below snippets show the equivalent in RecycleView. There are two examples in Kivy documentation for RecycleView.

The snippets below show the equivalent using RecycleView.

Snippets: kv file

RecycleView:
    id: x

    viewclass: 'Label'

    RecycleBoxLayout:
        default_size: None, dp(26)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

Snippets: py file

x.data.append({'text‘: 'frank’)

Examples

The following three examples illustrate Kivy RecycleView.

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior

Builder.load_string('''
<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    id: rv

    # Optional: ScrollView attributes
    bar_width: 5
    bar_color: 1, 0, 0, 1   # red
    bar_inactive_color: 0, 0, 1, 1   # blue
    effect_cls: "ScrollEffect"
    scroll_type: ['bars', 'content']

    viewclass: 'SelectableLabel'

    SelectableRecycleBoxLayout:
        default_size: None, dp(26)
        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 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


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]
        self.data.insert(0, {'text': 'frank'})
        self.data.append({'text': 'liam'})


class TestApp(App):
    title = 'Kivy RecycleView Demo'

    def build(self):
        return RV()


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

Output

Result

ikolim
  • 15,721
  • 2
  • 19
  • 29