Dima  Kudosh
Dima Kudosh

Reputation: 7386

Kivy infinite scroll

I want to create infinite scroll for my app. It's my code:

from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import AsyncImage


IMAGES_URLS = ['https://upload.wikimedia.org/wikipedia/commons/c/c3/Jordan_by_Lipofsky_16577.jpg' for _ in range(5)]


def upload_images(widget):
    layout = widget.children[0]
    layout_childrens = len(layout.children)
    for url in IMAGES_URLS:
        img = AsyncImage(source=url, size_hint_y=None, height=240)
        layout.add_widget(img)
    widget.scroll_y = 100 - (100 * layout_childrens / (layout_childrens + len(IMAGES_URLS)))


class InfinityScrollView(ScrollView):
    def on_scroll_move(self, touch):
        if self.scroll_y < 0:
            upload_images(self)
        return super(InfinityScrollView, self).on_scroll_move(touch)


class InfiniteScrollApp(App):
    def build(self):
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter('height'))
        for url in IMAGES_URLS:
            img = AsyncImage(source=url, size_hint_y=None,
                             height=240)
            layout.add_widget(img)
        root = InfinityScrollView(size_hint=(None, None), size=(400, 400),
                                  pos_hint={'center_x': .5, 'center_y': .5})
        root.add_widget(layout)
        return root


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

I overrode on_scroll_move method and when scroll on the bottom I called upload_images method that adds new images.

It's work fine but I got problem that scroll position stay on bottom after image loading, but I wont to move it to the first loaded images.

I tried to set correct value to scroll_y but it doesn't work, maybe I also must call some method or change other variables. Any advices?

Upvotes: 2

Views: 836

Answers (1)

Dima  Kudosh
Dima Kudosh

Reputation: 7386

I found solution, I needed to override 2 variables (scroll_y and effect_y). It's issue on github where I found solution. It's my fixed code.

def upload_images(self):
    layout = self.children[0]
    layout_childrens = len(layout.children)
    for url in IMAGES_URLS:
        img = AsyncImage(source=url, size_hint_y=None, height=240)
        layout.add_widget(img)
    bar_position = layout_childrens / (layout_childrens + len(IMAGES_URLS))
    self.scroll_y = 100 - 100 * bar_position
    self.effect_y.value = self.effect_y.min - self.effect_y.min * bar_position

Upvotes: 3

Related Questions