Last active
August 29, 2015 14:22
-
-
Save nrbrd/63404622dfb6e71534f5 to your computer and use it in GitHub Desktop.
Selectable Kivy-garden RecycleView experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.lang import Builder | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.uix.behaviors import CompoundSelectionBehavior | |
from kivy.uix.relativelayout import RelativeLayout | |
from kivy.uix.gridlayout import GridLayout | |
from kivy.uix.label import Label | |
from kivy.app import App | |
from recycleview import RecycleView | |
from kivy.properties import ObjectProperty | |
from kivy.properties import ListProperty | |
from kivy.properties import BooleanProperty | |
from kivy.core.window import Window | |
Builder.load_string( | |
''' | |
#:kivy 1.9 | |
#:import LinearRecycleLayoutManager recycleview.LinearRecycleLayoutManager | |
<ItemLabel>: | |
height: 35 | |
halign: 'left' | |
valign: 'middle' | |
text_size: self.size | |
padding_x: 15 | |
shorten: True | |
shorten_from: 'right' | |
canvas.before: | |
Color: | |
rgba: 47 / 255., 167 / 255., 212 / 255., 1 | |
Rectangle: | |
pos: self.x, self.y - 1 | |
size: self.width, 1 | |
Color: | |
rgba: 1, 1, 1, 0.1 if self.is_selected else 0 | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
<-CustomRecycleView>: | |
view_layout: layout | |
ScrollView: | |
id: sv | |
on_scroll_y: root.refresh_from_scroll("y") | |
on_scroll_x: root.refresh_from_scroll("x") | |
on_size: root.request_layout(full=True) | |
CustomRecycleViewLayout: | |
id: layout | |
size_hint: 1, None | |
<RootWidget>: | |
rv: rv | |
CustomRecycleView: | |
size_hint_x: 0.2 | |
x: 200 | |
id: rv | |
layout_manager: LinearRecycleLayoutManager(orientation="vertical") | |
data: root.data | |
viewclass: "ItemLabel" | |
canvas.before: | |
Color: | |
rgba: 1, 1, 1, 0.1 | |
Rectangle: | |
size: self.size | |
pos: self.parent.pos | |
''') | |
class CustomRecycleView(RecycleView): | |
view_layout = ObjectProperty(None) | |
class CustomRecycleViewLayout(CompoundSelectionBehavior, RelativeLayout): | |
def __init__(self, **kwargs): | |
super(CustomRecycleViewLayout, self).__init__(**kwargs) | |
keyboard = Window.request_keyboard(None, self) | |
keyboard.bind(on_key_down=self.select_with_key_down) | |
def select_node(self, node): | |
node.is_selected = True | |
node_src, idx_src = self._reslove_last_node() | |
self.parent.scroll_to(node) | |
return super(CustomRecycleViewLayout, self).select_node(node) | |
def deselect_node(self, node): | |
node.is_selected = False | |
super(CustomRecycleViewLayout, self).deselect_node(node) | |
class ItemLabel(Label): | |
is_selected = BooleanProperty(False) | |
bh = ObjectProperty(None) | |
def on_touch_down(self, touch): | |
if self.collide_point(*touch.pos): | |
self.bh.select_with_touch(self, touch) | |
return True | |
return False | |
class RootWidget(FloatLayout): | |
data = ListProperty([]) | |
rv = ObjectProperty(None) | |
def __init__(self, **kwargs): | |
super(RootWidget, self).__init__(**kwargs) | |
self.data = self.gen_data() | |
def gen_data(self): | |
for l in 'qwertyuiopasdfghjklzxcvbnm': | |
item = {'text': l, 'bh': self.rv.view_layout} | |
yield item | |
class MainApp(App): | |
def build(self): | |
return RootWidget() | |
if __name__ == '__main__': | |
MainApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment