Advertisement
ElliotDG

Untitled

Mar 13th, 2025
116
0
13 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.animation import Animation
  5. from kivy.core.window import Window
  6. from kivy.metrics import dp
  7. from kivy.clock import Clock, mainthread
  8. from kivy.properties import BooleanProperty, ListProperty
  9.  
  10. Window.size = 400, 600
  11.  
  12. kv = r'''
  13. PullDown:
  14.    orientation:"vertical"
  15.    Label:
  16.        id:btn
  17.        text:"Expandable Label"
  18.        size_hint_y: None
  19.        height:0
  20.        opacity:0
  21.    RecycleView:
  22.        id:rv
  23.        data: root.data
  24.        viewclass:"Button"
  25.        effect_cls: 'ScrollEffect'
  26.        RecycleGridLayout:
  27.            id:bl
  28.            cols:3
  29.            default_size_hint: 1, None
  30.            default_height: dp(101)
  31.            size_hint_y: None
  32.            size: self.minimum_size
  33. '''
  34.  
  35.  
  36. class PullDown(BoxLayout):
  37.     rv_open = BooleanProperty(False)  # used to create an event
  38.     data = ListProperty([{'text': str(x)} for x in range(18)])
  39.  
  40.     def __init__(self, **kwargs):
  41.         super().__init__(**kwargs)
  42.         self._animation_ongoing = False
  43.  
  44.     def on_touch_down(self, touch):
  45.         if self.collide_point(*touch.pos):
  46.             touch.grab(self)
  47.         if self.rv_open:
  48.             self.collapse(touch)
  49.         return super().on_touch_down(touch)
  50.  
  51.     def on_touch_move(self, touch):
  52.         if (touch.grab_current is self) and (not self._animation_ongoing):
  53.             if touch.dy < -10:
  54.                 self.expand(touch)
  55.                 return super().on_touch_up(touch)
  56.         if self._animation_ongoing:
  57.             return True
  58.         return super().on_touch_move(touch)
  59.  
  60.     def on_touch_up(self, touch):
  61.         if touch.grab_current is self:
  62.             touch.ungrab(self)
  63.         return super().on_touch_up(touch)
  64.  
  65.     # @mainthread
  66.     def expand(self, touch):
  67.         self._animation_ongoing = True
  68.         # super().on_touch_up(touch)
  69.         anim = Animation(
  70.             height=dp(100),
  71.             opacity=1,
  72.             d=0.3, t="out_cubic"
  73.         )
  74.  
  75.         def on_animation_complete(*args):
  76.             self._animation_ongoing = False
  77.             self.rv_open = True
  78.  
  79.         anim.bind(on_complete=on_animation_complete)
  80.         anim.start(self.ids.btn)
  81.  
  82.     # @mainthread
  83.     def collapse(self, touch):
  84.         self._animation_ongoing = True
  85.         anim = Animation(
  86.             height=0,
  87.             opacity=0,
  88.             d=0.3, t="out_cubic"
  89.         )
  90.  
  91.         def on_animation_complete(*args):
  92.             self._animation_ongoing = False
  93.             self.rv_open = False
  94.             self.ids.rv.scroll_y = 1  # set scroll_y to None
  95.  
  96.         anim.bind(on_complete=on_animation_complete)
  97.         anim.start(self.ids.btn)
  98.  
  99.  
  100. class Test(App):
  101.     def build(self):
  102.         self.title = 'Pull down touch'
  103.         return Builder.load_string(kv)
  104.  
  105.  
  106. Test().run()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement