Kivy RecycleView-元素从底部显示

时间:2019-04-09 05:51:43

标签: python python-3.x kivy

我对RecycleView有疑问。我想我不得不犯一些小错误,因为我没有在Google上找到同样问题的人。

问题是,当我将新项目添加到RecycleView列表中时,它们从底部开始出现,如下面的GIF所示:

GIF

这是我的代码的摘录:

class SelectableButton(RecycleDataViewBehavior, Button):
    index = None

    def refresh_view_attrs(self, rv, index, data):
        """ Catch and handle the view changes """
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_press(self):
        self.parent.parent.parent.chose_device(self.device)
        print("Connecting to %d" % self.index)

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    pass

class DeviceList(RecycleView):
    def __init__(self, **kwargs):
        super(DeviceList, self).__init__(**kwargs)
        self.data = []

class DiscoveryForm(BoxLayout):
(...)

    def new_device_discovered(self):
        self.disc_list_prop.data = []
        devices = self.discovery.get_discovered_devices()
        for i, device in enumerate(devices):
            self.disc_list_prop.data.append({'text': "Device " + device.mac, 'id': str(i), 'device': device})


    def chose_device(self, text):
        (...)

app.kv:

RootForm:
    Label:
        text: "Initialization"

<DiscoveryForm>:
    orientation: "vertical"
    platform_label_prop: platform_label
    disc_list_prop: disc_list
    Label:
        id: platform_label
        height: "40dp"
        size_hint_y: None
        text: "Discovery"
    Button:
        text: "Search for devices"
        height: "100dp"
        size_hint_y: None
        on_press: root.start_searching()
    DeviceList:
        id: disc_list
    Label:
        size_hint_y: 1
    Button:
        text: "Go to driver"
        size_hint_y: None
        height: "50dp"
        on_press: root.parent.show_driver_form()

<SelectableButton>:

<DeviceList>:
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        orientation: 'vertical'   
        default_size_hint: 1, 1

我试图消除第一个按钮和第一个发现的设备按钮之间的间隙,但是没有间隙。我也尝试过vlayout,但没有帮助。

1 个答案:

答案 0 :(得分:1)

  • default_size_hint: 1, 1替换为default_size_hint: 1, None
  • 添加size_hint_y: None
  • 设置最低高度height: self.minimum_height

摘要

<DeviceList>:
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        orientation: 'vertical'   
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height