簡體   English   中英

Kivy ScrollView 不會滾動到較低的內容

[英]Kivy ScrollView won't scroll to lower content

我有一個 ScrollView,我正在用 AsyncImage 對象填充它。 但是,我的 ScrollView 似乎認為沒有什么可向下滾動的,並將其視為過度滾動。 我認為這是因為我在實例化后添加到它的子布局,但不知道如何修復它。

錯誤行為: https : //i.imgur.com/OjcR2RY.mp4

現在,我已經考慮完全禁用過度滾動行為,但我需要它為未來的功能正常工作。

主文件

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

簡單的.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'))

像魅力一樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM