Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.app import App
- from kivy.lang import Builder
- from kivy.uix.boxlayout import BoxLayout
- from kivy.animation import Animation
- from kivy.core.window import Window
- from kivy.metrics import dp
- from kivy.clock import Clock, mainthread
- from kivy.properties import BooleanProperty, ListProperty
- Window.size = 400, 600
- kv = r'''
- PullDown:
- orientation:"vertical"
- Label:
- id:btn
- text:"Expandable Label"
- size_hint_y: None
- height:0
- opacity:0
- RecycleView:
- id:rv
- data: root.data
- viewclass:"Button"
- effect_cls: 'ScrollEffect'
- RecycleGridLayout:
- id:bl
- cols:3
- default_size_hint: 1, None
- default_height: dp(101)
- size_hint_y: None
- size: self.minimum_size
- '''
- class PullDown(BoxLayout):
- rv_open = BooleanProperty(False) # used to create an event
- data = ListProperty([{'text': str(x)} for x in range(18)])
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self._animation_ongoing = False
- def on_touch_down(self, touch):
- if self.collide_point(*touch.pos):
- touch.grab(self)
- if self.rv_open:
- self.collapse(touch)
- return super().on_touch_down(touch)
- def on_touch_move(self, touch):
- if (touch.grab_current is self) and (not self._animation_ongoing):
- if touch.dy < -10:
- self.expand(touch)
- return super().on_touch_up(touch)
- if self._animation_ongoing:
- return True
- return super().on_touch_move(touch)
- def on_touch_up(self, touch):
- if touch.grab_current is self:
- touch.ungrab(self)
- return super().on_touch_up(touch)
- # @mainthread
- def expand(self, touch):
- self._animation_ongoing = True
- # super().on_touch_up(touch)
- anim = Animation(
- height=dp(100),
- opacity=1,
- d=0.3, t="out_cubic"
- )
- def on_animation_complete(*args):
- self._animation_ongoing = False
- self.rv_open = True
- anim.bind(on_complete=on_animation_complete)
- anim.start(self.ids.btn)
- # @mainthread
- def collapse(self, touch):
- self._animation_ongoing = True
- anim = Animation(
- height=0,
- opacity=0,
- d=0.3, t="out_cubic"
- )
- def on_animation_complete(*args):
- self._animation_ongoing = False
- self.rv_open = False
- self.ids.rv.scroll_y = 1 # set scroll_y to None
- anim.bind(on_complete=on_animation_complete)
- anim.start(self.ids.btn)
- class Test(App):
- def build(self):
- self.title = 'Pull down touch'
- return Builder.load_string(kv)
- Test().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement