我在使用Kivy的ListView时遇到了一些麻烦

时间:2016-06-19 09:34:54

标签: python kivy

所以我在使用ListView时遇到了一些麻烦。我一直在尝试做的是对ebay进行API调用。然后我用结果设置一个ListView。但是,这是问题所在。我第一次进行搜索并将其带到结果页面时,它只显示一个结果。如果我向下滚动,我会看到另一个结果或两个弹出,但它们会立即消失。然后我将返回主页并执行完全相同的搜索,但是,这次进入结果页面时,它会列出更多内容。如果我向下滚动一切看起来一切都很好,但随后间距会搞砸,会有一个巨大的虚无之间,那么结果会更加接近。 Here是一些图片,可以更好地解释这个问题(对不起,我把代码快速地拼凑起来测试它......虽然我一直试图解决这个问题)

kv文件

#: import ListItemLabel kivy.uix.listview.ListItemLabel
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import CompositeListItem kivy.uix.listview.CompositeListItem
#: import AsyncImage kivy.uix.image.AsyncImage
#: import SelectableView kivy.uix.listview.SelectableView
#: import BoxLayout kivy.uix.boxlayout.BoxLayout
#: import main main

ScreenManagement:

<ScreenManagement>:
    SearchForm
    ResultPage



<SearchForm>:
    name: "search_screen"
    search_box:search_box
    search_button:search_button
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: "FLEA MARKET FLIP"
            font_size: 45
            pos_hint: {"center_x":0.5, "center_y":1}
        BoxLayout:
            orientation: 'vertical'
            size_hint_y: None
            height: '100dp'
            TextInput:
                id: search_box
                text: ""
                size_hint_x: 0.5
                pos_hint: {"center_x":0.5, "center_y":0.5}
            Button:
                text: "Price It!"
                id:search_button
                on_release:
                root.manager.get_screen("result_page").results_list.adapter.data.clear()
                root.manager.get_screen("result_page").results_list.adapter.data.extend([s for s in app.root.ebay(search_box.text)])
                root.manager.get_screen("result_page").do_layout()
                root.manager.get_screen("result_page").results_list._trigger_reset_populate()
                app.root.current = "result_page"




<ResultPage>:
    name: "result_page"
    orientation: 'vertical'
    results_list:results_list
    ListView:
        padding:(100,110)
        id: results_list
        adapter: ListAdapter(data=[], cls=main.Ebay_Item, args_converter=root.args_converter, allow_empty_selection=False)
    FloatLayout:
        Button:
            size_hint: [0.2,0.1]
            spacing: 50
            pos_hint: {"x":0, "y":0}
            text: "Back"
            on_release:
                app.root.current = "search_screen"



<Ebay_Item>:
    orientation: "vertical"
    cols: 3
    #spacing: 60
    row_force_default: True
    row_default_height: 40
    Label:
        text: root.results[0]
        size: self.texture_size
        text_size: self.width, self.height
    Label:
        text: root.results[1]
        size: self.texture_size
    AsyncImage:
        allow_stretch: True
        size_hint_y: None
        size_hint_x: None
        source: root.results[2]
        size: self.texture_size

main.py

from kivy.uix.gridlayout import GridLayout
from kivy.properties import ListProperty
from scripts.ebay import ebay_search
from kivy.uix.listview import SelectableView
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.app import App


class PriceItApp(App):
    pass


class ScreenManagement(ScreenManager):
    def ebay(self, data):
        return ebay_search(data)


class SearchForm(Screen):
    pass


class ResultPage(Screen):
    def args_converter(self, index, data_item):
        title, price, picture = data_item
        return {"results": [title, price, picture]}


class Ebay_Item(SelectableView, GridLayout):
    results = ListProperty()


if __name__ == "__main__":
    PriceItApp().run()

0 个答案:

没有答案