'How to use Python Kivy RecycleView in another layout without using Kivy language?
I want to use RecycleView in another layout without using Kivy language, but I couldn't display the data on the Window. What did I write bad or miss in my code?
I know kv language is good, but I don't want to use it in this small project.
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
class MainWidget(FloatLayout):
def __init__(self):
super().__init__()
self.create_layouts()
def create_layouts(self):
self.create_recycle_view()
def create_recycle_view(self):
recycle_view = RecycleView()
recycle_view.data = [{'text': str(x)} for x in range(20)]
recycle_view.viewclass = 'Label'
recycle_box_layout = RecycleBoxLayout()
recycle_box_layout.size_hint = (1, 0.2)
recycle_view.add_widget(recycle_box_layout)
self.add_widget(recycle_view)
class MainApp(App):
def build(self):
return MainWidget()
Solution 1:[1]
There seem to be some strange side effects with some of the RecycleView
classes, resulting in viewclass
being set to None
. So setting viewclass
must be done later than in your code. Also, I believe that the building of the RecycleView
should not be done in the __init__()
method of the MainWidget
. So, based on that, a modified version of your code that works is:
from kivy.app import App
from kivy.clock import Clock
from kivy.metrics import dp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
class MainWidget(FloatLayout):
def create_layouts(self):
self.create_recycle_view()
def create_recycle_view(self):
recycle_box_layout = RecycleBoxLayout(default_size=(None, dp(56)), default_size_hint=(1, None),
size_hint=(1, None), orientation='vertical')
recycle_box_layout.bind(minimum_height=recycle_box_layout.setter("height"))
recycle_view = RecycleView()
recycle_view.add_widget(recycle_box_layout)
recycle_view.viewclass = 'Label'
self.add_widget(recycle_view)
recycle_view.data = [{'text': str(x)} for x in range(20)]
class MainApp(App):
def build(self):
Clock.schedule_once(self.add_rv)
return MainWidget()
def add_rv(self, dt):
self.root.create_layouts()
MainApp().run()
This code uses Clock.schedule_once()
(in the build()
method of the App
) to build the RecycleView
. Also, the height
of the RecycleBoxLayout
is bound to its minimum_height
property.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John Anderson |