Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Last active May 15, 2022 15:04
Show Gist options
  • Save tshirtman/dc08ffe19a5b6ae53f6f33c0827fd073 to your computer and use it in GitHub Desktop.
Save tshirtman/dc08ffe19a5b6ae53f6f33c0827fd073 to your computer and use it in GitHub Desktop.
An example of how to create an infinite scroll with recycleview (appending data as user scrolls)
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.app import App
from random import sample
from string import ascii_letters as letters
KV = '''
<MyButton@Button>:
on_press: print(self.text)
RecycleView:
data: app.data
on_scroll_y: app.fill_data(self, box)
viewclass: 'MyButton'
bar_width: 20
RecycleBoxLayout:
id: box
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
default_size: 0, 100
'''
class DataApp(App):
data = ListProperty([])
def build(self):
self.fetch_data(10)
print(self.data)
return Builder.load_string(KV)
def fill_data(self, rv, box):
top = (box.top - rv.y) / box.height
if rv.scroll_y * box.height < rv.height / 2:
self.fetch_data(10)
y = top + rv.height
rv.scroll_y = y / box.height
# rv.effect_y.update(y / box.height)
def fetch_data(self, length):
data = []
n = len(self.data)
for i in range(length):
data.append({'text': '{}: '.format(n + i) + ''.join(sample(letters, 10))})
self.data.extend(data)
if __name__ == '__main__':
DataApp().run()
@tshirtman
Copy link
Author

@AssemRad
Copy link

AssemRad commented Sep 11, 2020

when you scroll about a page and a half .
for me on windows laptop the scroll view contains 6 visible items (0-5).
while scrolling down, I have slowed down scrolling at the moment when index 12 about to disappear.
Then with just a single scrolling step, instead of being 13 as the first appearing element.
It jumped to be starting from 17

Please have a look at the attached animation. focus on the frame when element index 12 is about to disappear.
capture

Thank you for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment