如何在屏幕上使用RecycleView

时间:2019-05-11 16:11:00

标签: python python-3.x kivy kivy-language

我可以尝试学习奇异果,并且我有迷你项目。我的项目中有很多屏幕。我在执行项目时遇到问题。我想显示20个问题,并在每个问题中都显示一个复选框,但是如果屏幕上适合20个问题,则图像将不是很好。 如果我可以向下滚动屏幕,我想使用滚动方式解决此问题,但是我无法在屏幕中使用RecycleView,如何在屏幕中使用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 个答案:

答案 0 :(得分:0)

  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. 您可以在屏幕中使用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修复的:herehere