Kivy ScrollView 不会滚动到较低的内容

Kivy ScrollView won't scroll to lower content

我有一个用 AsyncImage 对象填充的 ScrollView。但是,我的 ScrollView 似乎认为没有什么可以向下滚动的,并且将其视为过度滚动。我认为这是因为我在实例化后添加到它的子布局,但不知道如何修复它。

错误行为:https://i.imgur.com/OjcR2RY.mp4

现在,我考虑过完全禁用过度滚动行为,但我需要它才能在未来的功能中正常工作。

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

当您使用 GridLayoutBoxLayout 时,您应该指定高度,否则它们只会采用父尺寸

但是当我们在 ScrollView 小部件中使用它时,您应该使用 minimum_height

使高度等于 GridLayout 子级

在你的情况下,解决方案是

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

我必须将高度设置为与 GridLayout 的内容成比例。

我通过添加

实现了这一点

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

工作得很好。