如何在 Screen 中使用 RecycleView

How to use RecycleView in Screen

我正在尝试学习 Kivy 并制作了一个包含许多屏幕的小项目。第一个问题是,我想显示 20 个问题,每个问题都有一个复选框。但是,如果屏幕上显示 20 个问题,图像就不会很好。为了解决这个问题 我想使用滚动。所以如果你可以向下滚动,问题就解决了。但是我不能在 Screen 中使用 RecycleView。

那么如何在 Screen 中使用 RecycleView?

我在网上看了其他类似的问题,但不是我不明白就是不是我需要的。

编辑: 我的 .kv 文件中的代码生成的图像这就是我想要的。我只需要添加一个向下滚动功能。我该怎么做?

我的 .kv 文件

<SkillChose>:
    name:"skill"
    GridLayout:
        cols:1
        GridLayout:
            cols:2
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
  1. 您可以使用 ScrollView 上下滚动 GridLayout。 这是一个例子:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label

KV = """
BoxLayout

    ScrollView:
        # uncomment the line below if items text looks a bit blurry
        #on_pos: self.pos = tuple(map(int, args[1]))

        size: self.size

        GridLayout:
            id: grid
            size_hint_y: None
            row_default_height: '50sp'
            height: self.minimum_height
            cols:2

            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"try"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
            Label:
                text:"pls"
            CheckBox:
            Label:
                text:"more"
            CheckBox:
"""
class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

MyApp().run()
  1. 您可以在 Screens 中使用 RecycleView。 我为你写了一个简单的例子:
from kivy.app import App
from kivy.lang import Builder

from kivy.uix.recycleview import RecycleView

KV = """
ScreenManager
    Screen
        name: 's1'
        BoxLayout
            Button
                text: 'go to screen 2'
                on_press: root.current = 's2'
            RV

    Screen
        name: 's2'
        BoxLayout
            Button
                text: 'go to screen 1'
                on_press: root.current = 's1'
            RV


<RV>
    viewclass: 'Label'
    RecycleBoxLayout
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
"""

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]

class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

MyApp().run()

但是使用 RecycleView 比使用滚动视图要困难一些。

请注意 - 将 RecycleView 与复选框一起使用时有一个意想不到的功能 - 滚动时所选复选框的显示不正确。 这是由 tshirtman 修复的:here and here