Skip to content

Instantly share code, notes, and snippets.

@matham
Created July 22, 2015 21:00
Show Gist options
  • Save matham/988f35e13c2ec936f520 to your computer and use it in GitHub Desktop.
Save matham/988f35e13c2ec936f520 to your computer and use it in GitHub Desktop.
from functools import partial
from kivy.config import Config
Config.set('modules', 'inspector', '')
from kivy.garden.recycleview import RecycleView, RecycleViewMixin, LayoutChangeException
from kivy.lang import Builder
from kivy.app import App
from kivy.factory import Factory
from kivy.animation import Animation
KV = """
<ContactItem>:
canvas.before:
Color:
rgba: 1, 1, 1, 0.5
Rectangle:
pos: self.pos
size: self.size
color: 0, 0, 0, 0.5
BoxLayout:
rv: rv
orientation: "vertical"
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "add"
on_release: app.add_item(len(rv.data))
Button:
text: "remove"
on_release: app.remove_item(0)
RecycleView:
id: rv
key_viewclass: 'viewclass'
key_size: 'height'
"""
def update_anim(rv, index, anim, widget, progress):
rv.data[index]['height'] = progress * 70
rv.ask_refresh_from_data(extent='data_size')
def finish_anim(rv, index, anim, widget):
update_anim(rv, index, anim, widget, 1)
print 'anim finished', rv.data[index]
print '\n\n'
class ContactItem(RecycleViewMixin, Factory.Label):
def refresh_view_layout(self, rv, index, pos, size, viewport):
item = rv.data[index]
if not item['visible']:
item['visible'] = True
self.height = 0
self.opacity = 0
self.rv = rv
self.data_index = index
print 'animate', item
anim = Animation(opacity=1, d=0.2)
anim.bind(on_progress=partial(update_anim, rv, index), on_complete=partial(finish_anim, rv, index))
anim.start(self)
self.size = 0, 0
raise LayoutChangeException()
self.pos = pos
self.size = size
def on_anim(self, rv, index, prop, _, value, *args):
rv.data[index][prop] = value
#rv.ask_refresh_viewport()
def on_anim_complete(self, h_callback, o_callback, *args):
self.anim_in.unbind(on_complete=self.a_comp_callb)
self.unbind(height=h_callback, opacity=o_callback)
class RecycleViewApp(App):
def build(self):
return Builder.load_string(KV)
def add_item(self, index):
data = self.root.rv.data
data.insert(index, {#'opacity': 0,
'visible': False,
'height': 0,
'viewclass': 'ContactItem',
'text': 'x' + str(len(data))})
def remove_item(self, index):
data = self.root.rv.data
data.pop(0)
RecycleViewApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment