关于recycleview的一些清楚的解释

时间:2019-10-06 18:29:43

标签: python kivy

晚上好,我试图了解Kivy,并且陷入了recycleView小部件的困境。我在网上搜索了很多内容,发现穷人对此的解释(或对我不太清楚)。 所以,我决定尝试阅读有效的代码,但对我来说似乎仍然很复杂

我是GUI设计的初学者,了解有关OOP的理论,并且具有基本的Python技能。 我正在使用Kivy 1.11.0。

这是我想要理解的代码; 有main.py文件和example.kv文件:

##############################################################################
#main.py 
##############################################################################

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.floatlayout import FloatLayout



# Create a Custom ButtonLabel that can use on_press
class ButtonLabel(ButtonBehavior, Label):

    # App.get_running_app() lets us traverse all the way through our app from
    # the very top, which allows us access to any id. In this case we are accessing
    # the content of our selected_list_view of our app
    @property
    def selected_list_content(self):
        return App.get_running_app().root.ids.selected_list.ids.content

    # And in this case, we're accessing the content of our deselected_list_view
    @property
    def deselected_list_content(self):
        return App.get_running_app().root.ids.deselected_list.ids.content

    # This is our callback that our Label's will call when pressed
    def change_location(self):

        # If the label's parent is equal* the selected list, we remove the label from its
        # parent, and then we add it to the other list
        if self.parent == self.selected_list_content:
            self.parent.remove_widget(self)
            self.deselected_list_content.add_widget(self)

        # If the label's parent is not the selected list, then it is the deselected list
        # so we remove it from its parent and add it to the selected list
        else:
            self.parent.remove_widget(self)
            self.selected_list_content.add_widget(self)

    #* Note: Kivy uses weak references. This is why we use ==, and not 'is'

# We create a CustomRecycleView that we will define in our kv file      
class CustomRecycleView(RecycleView):
    pass

class MainWindow(FloatLayout):
    pass

class ExampleApp(App):

    def build(self):
        # We create an instance of the MainWindow class, so we can access its id
        # to import our list. Otherwise we would have nothing to add the list too
        main_window = MainWindow()
        importedlist = ['Novella Varela', 'Caroll Faircloth', 'Douglas Schissler',
                'Rolande Hassell', 'Hayley Rivero', 'Niesha Dungy', 'Winfred Dejonge', 'Venetta Milum']

        # We create a Label for each Name in our imported list, and then add it
        # to the content of selected list as a default
        # I'm sure you'll be importing our own lists in a different manner
        # This is just for the example
        for name in importedlist:
            NameLabel = ButtonLabel(text=(name))
            main_window.ids.selected_list.ids.content.add_widget(NameLabel)
        return main_window

if __name__ == '__main__':
    ExampleApp().run()


##############################################################################
#example.kv
##############################################################################

#:kivy 1.11.0

# We create a reference to the ButtonLabel class in our py file
<ButtonLabel>:
    # We add our callback to our ButtonLabels on press event, on_press
    on_press: root.change_location()
<CustomRecycleView>:
    # We create a GridLayout to store all of the content in our RecycleView
    GridLayout:
        # We give it the id content so we can define the two property values in
        # ButtonLabel class in the py file
        id: content
        size_hint_y: None

        # One column because we want it to be vertical list list
        cols: 1

        # This set up, as well as size_hint_y set to None
        # is so we can scroll vertically without issue
        row_default_height: 60
        height: self.minimum_height

<MainWindow>:
    # We then create two instances of our CustomRecycleView, give them the ids
    # referenced by the ButtonLabel methods as well as give them equal share of the
    # screen space so they do not step on each others toes
    # The canvas here is just for prototyping purposes to make sure they are the
    # properly defined sizes. You can do whatever with them you would like tbh.
    CustomRecycleView:
        id: selected_list
        size_hint: 1, .5
        pos_hint: {'x': 0, 'y': .5}
        canvas:
            Color:
                rgba: 100, 0, 0, .2
            Rectangle:
                size: self.size
                pos: self.pos
    CustomRecycleView:
        id: deselected_list
        size_hint: 1, .45
        canvas:
            Color:
                rgba: 0, 0, 100, .2
            Rectangle:
                size: self.size
                pos: self.pos

现在,我对recycleWidget的想法是包含其他小部件,但允许它们轻松刷新和更好的数据。 这是正确的吗?

如果是这样,我将无法理解代码中确切的位置。您如何更改此示例中的“按钮标签”文本?我试图在change_location()中做些什么,但这没用。

是否有任何特定资源可以查找,以便更好地了解recycleView?对我来说,这是迄今为止最复杂的事情。

谢谢!

0 个答案:

没有答案