-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_rv.py
180 lines (155 loc) · 6.08 KB
/
smart_rv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from kivy.factory import Factory as F
from kivy.animation import Animation
class SmartRV(F.RecycleView):
"""
This RecycleView implements a smart scroll behavior.
You can select the item you want to scroll to by using the
function to scroll to an item called `scroll_to_item`. You will
be able to scroll to the item by using the item's index, and you can
select among three different scroll behaviors: `scroll_to_top`,
`scroll_to_center` and `scroll_to_bottom`.
"""
orientation = F.OptionProperty("vertical", options=["vertical", "horizontal"])
children_height = F.NumericProperty()
children_width = F.NumericProperty()
vertical_behavior = F.OptionProperty(
"scroll_to_top",
options=["scroll_to_top", "scroll_to_center", "scroll_to_bottom"],
)
horizontal_behavior = F.OptionProperty(
"scroll_to_left",
options=["scroll_to_left", "scroll_to_center", "scroll_to_right"],
)
scroll_duration = F.NumericProperty(1)
def scroll_to_item(self, index):
"""
Scroll to the item at the given index.
:param index: The index of the item to scroll to.
"""
if not self.data:
return
if self.orientation == "vertical":
N = len(self.data) # number of items
h = self.children_height # height of each item
H = self.height # height of the RecycleView
if self.vertical_behavior == "scroll_to_top":
scroll_y = 1 - (index * h) / (N * h - H)
elif self.vertical_behavior == "scroll_to_center":
scroll_y = 1 - (index * h - H / 2 + h / 2) / (N * h - H)
elif self.vertical_behavior == "scroll_to_bottom":
scroll_y = 1 - (index * h - H + h) / (N * h - H)
Animation(scroll_y=scroll_y, d=self.scroll_duration).start(self)
elif self.orientation == "horizontal":
N = len(self.data) # number of items
w = self.children_width # width of each item
W = self.width # width of the RecycleView
if self.horizontal_behavior == "scroll_to_left":
scroll_x = (index * w) / (N * w - W)
elif self.horizontal_behavior == "scroll_to_center":
scroll_x = (index * w - W / 2 + 1 / 2 * w) / (N * w - W)
elif self.horizontal_behavior == "scroll_to_right":
scroll_x = (index * w - W + w) / (N * w - W)
Animation(scroll_x=scroll_x, d=self.scroll_duration).start(self)
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
# fmt: off
kv = Builder.load_string("""
#:import random random
#:import Animation kivy.animation.Animation
BoxLayout:
orientation: 'vertical'
BoxLayout:
Button:
text: "Click to \\nscroll"
font_size: sp(42)
halign: 'center'
color: 0,1,0,1
on_release:
rv.data = [{'text': str(x)} for x in range(100)]
random_number = random.randint(0, 99)
self.text = str(random_number)
rv.scroll_to_item(random_number)
for value in rv.data: value['color'] = [1, 1, 1, 1]
if random_number > 0: rv.data[random_number]['color'] = [0, 1, 0, 1]
else: rv.data[0]['color'] = [0, 1, 0, 1]
ToggleButton:
text: "TOP"
group: "vertical_behavior"
state: "down"
on_state:
if self.state == "down": rv.vertical_behavior = "scroll_to_top"
ToggleButton:
text: "CENTER"
group: "vertical_behavior"
on_state:
if self.state == "down": rv.vertical_behavior = "scroll_to_center"
ToggleButton:
text: "BOTTOM"
group: "vertical_behavior"
on_state:
if self.state == "down": rv.vertical_behavior = "scroll_to_bottom"
SmartRV:
id: rv
viewclass: 'Button'
data: [{'text': str(x)} for x in range(100)]
size_hint_y: None
height: dp(250)
children_height: dp(50)
RecycleBoxLayout:
default_size: None, dp(50)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
BoxLayout:
Button:
text: "Click to \\nscroll"
font_size: sp(42)
halign: 'center'
color: 1,0,0,1
on_release:
rv2.data = [{'text': str(x)} for x in range(100)]
random_number = random.randint(0, 99)
self.text = str(random_number)
rv2.scroll_to_item(random_number)
for value in rv2.data: value['color'] = [1, 1, 1, 1]
if random_number > 0: rv2.data[random_number]['color'] = [1, 0, 0, 1]
else: rv2.data[0]['color'] = [1, 0, 0, 1]
ToggleButton:
text: "LEFT"
group: "horizontal_behavior"
state: "down"
on_state:
if self.state == "down": rv2.horizontal_behavior = "scroll_to_left"
ToggleButton:
text: "CENTER"
group: "horizontal_behavior"
on_state:
if self.state == "down": rv2.horizontal_behavior = "scroll_to_center"
ToggleButton:
text: "RIGHT"
group: "horizontal_behavior"
on_state:
if self.state == "down": rv2.horizontal_behavior = "scroll_to_right"
SmartRV:
id: rv2
viewclass: 'Button'
data: [{'text': str(x)} for x in range(100)]
size_hint_x: None
width: dp(700)
children_width: dp(50)
orientation: 'horizontal'
pos_hint: {'center_x': 0.5}
RecycleBoxLayout:
default_size: dp(50), None
default_size_hint: None, 1
size_hint_x: None
width: self.minimum_width
orientation: 'horizontal'
""")
# fmt: on
class TestApp(App):
def build(self):
return kv
TestApp().run()