Home > Net >  Kivy ScrollView won't scroll to lower content
Kivy ScrollView won't scroll to lower content

Time:10-23

I have a ScrollView that I am populating with AsyncImage objects. However, my ScrollView seems to think that there is nothing to scroll down to, and treats it like over scroll. I think this is because I am adding to its child layout after it is instantiated, but don't know how to fix it.

Faulty behavior: https://i.imgur.com/OjcR2RY.mp4

Now, I've thought about disabling overscroll behavior alltogether, but I need it to work correctly for a future feature.

main.py

import gc

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import AsyncImage
from kivy.uix.widget import Widget


class ProviderWindow(Widget):

    def __init__(self, **kwargs):
        super(ProviderWindow, self).__init__(**kwargs)
        self.C_L = GridLayout(cols=3, spacing=0, size_hint=(1, None), pos=(0, 0))
    
    def search(self):

        # Clear any existing images inside the view when starting a fresh search
        for child in App.get_running_app().root.ids.image_scroll_view.children:
            del child
        App.get_running_app().root.ids.image_scroll_view.clear_widgets()
        gc.collect(generation=2)

        # GridView properties
        self.C_L.col_default_width = 500
        self.C_L.row_default_height = 500

        urls = self.gb.search()  # Get a list of URLs to load into AsyncImages (can simply be a list of Strings)

        # Adding All images to GridLayout
        for entry in urls:
            img = AsyncImage(source=entry, keep_ratio=True, allow_stretch=True)
            img.size_hint = (1, 1)
            self.C_L.add_widget(img)

        # Adding GridLayout to ScrollView
        self.ids.image_scroll_view.add_widget(self.C_L)


class SimpleApp(App):
    pass

Simple.kv

ProviderWindow:

<ProviderWindow>:
    BoxLayout:
        orientation: "vertical"
        size_hint: None, None
        size: (root.size[0], root.size[1])

        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 0.1

            Button:
                id:search_button
                text:"Search"
                on_press: root.search()
                size_hint: 0.3,None

            TextInput
                id:tags
                size_hint: 0.4, None

            Button:
                id:search_button
                text:"More"
                size_hint: 0.3,None

            Button:
                id:settings_button
                text:"Settings"
                size_hint: 0.2, None


        ScrollView:
            id: image_scroll_view
            do_scroll_x: False
            do_scroll_y: True

CodePudding user response:

when you use GridLayout or BoxLayout you should specify there height otherwise they just will take there parent size

but when we use it inside ScrollView widget you should make the height equal to the GridLayout children using minimum_height

in you case the solution is

self.C_L = GridLayout(cols=3, spacing=0, size_hint=(1, None), pos=(0, 0),height=self.minimum_height)

CodePudding user response:

I had to set the height to scale with the contents of the GridLayout.

I achieved this by adding

self.C_L.bind(minimum_height=self.C_L.setter('height'))

Worked like a charm.

  • Related