Skip to content

Instantly share code, notes, and snippets.

@tito
Last active February 6, 2018 13:20
Show Gist options
  • Save tito/3ef1f23fc72f332b437bd15963ac476d to your computer and use it in GitHub Desktop.
Save tito/3ef1f23fc72f332b437bd15963ac476d to your computer and use it in GitHub Desktop.
Cyclable RecycleView
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
kv = """
<Row@BoxLayout>:
canvas.before:
Color:
rgba: 0.5, 0.5, 0.5, 1
Rectangle:
size: self.size
pos: self.pos
value: ''
Label:
text: root.value
<Test>:
canvas:
Color:
rgba: 0.3, 0.3, 0.3, 1
Rectangle:
size: self.size
pos: self.pos
RecycleView:
id: rv
scroll_type: ['bars', 'content']
scroll_wheel_distance: dp(114)
bar_width: dp(10)
viewclass: 'Row'
on_scroll_y: root.check_scroll_y(self)
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
spacing: dp(2)
"""
Builder.load_string(kv)
class Test(BoxLayout):
cycle = NumericProperty(20)
def populate(self):
data = [
{"value": "Entry {}".format(x)}
for x in range(50)
]
data += data[:self.cycle]
self.ids.rv.data = data
def check_scroll_y(self, rv):
rv = self.ids.rv
lm = rv.layout_manager
if rv.scroll_y > 1:
# cycle_to_bottom
child = lm.view_opts[-self.cycle]
x, y = child["pos"]
w, h = child["size"]
sy = (y - rv.height + h) / (lm.height - rv.height - lm.spacing[1])
rv.scroll_y = sy
elif rv.scroll_y < 0:
child = lm.view_opts[self.cycle]
x, y = child["pos"]
w, h = child["size"]
sy = (y + h) / (lm.height - rv.height - lm.spacing[1])
rv.scroll_y = sy
class TestApp(App):
def build(self):
self.root = Test()
self.root.populate()
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment