【问题标题】:How to use RecycleView in Screen如何在屏幕中使用 RecycleView
【发布时间】:2019-05-11 16:11:00
【问题描述】:

我正在尝试学习 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:

【问题讨论】:

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


    【解决方案1】:
    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

    【讨论】:

    • 谢谢老兄第一个对我来说已经足够了,谢谢例如我看到了很多例子,但是这个很好非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多