Skip to content

Commit c8c3448

Browse files
committed
Add scroll_view.py
1 parent b55edd4 commit c8c3448

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

libs/pythonnative/pythonnative/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .page import Page
1515
from .picker_view import PickerView
1616
from .progress_view import ProgressView
17+
from .scroll_view import ScrollView
1718
from .search_bar import SearchBar
1819
from .stack_view import StackView
1920
from .switch import Switch
@@ -39,6 +40,7 @@
3940
"Page",
4041
"PickerView",
4142
"ProgressView",
43+
"ScrollView",
4244
"SearchBar",
4345
"StackView",
4446
"Switch",

libs/pythonnative/pythonnative/page.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ def on_restore_instance_state(self) -> None:
134134

135135
def navigate_to(self, page) -> None:
136136
# intent = jclass("android.content.Intent")(self.native_instance, page.native_class)
137-
intent = jclass("android.content.Intent")(self.native_instance,
138-
jclass("com.pythonnative.pythonnative.SecondActivity"))
137+
intent = jclass("android.content.Intent")(
138+
self.native_instance,
139+
jclass("com.pythonnative.pythonnative.SecondActivity"),
140+
)
139141
self.native_instance.startActivity(intent)
140142

141143
else:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from abc import ABC, abstractmethod
2+
from .utils import IS_ANDROID
3+
from .view import ViewBase
4+
5+
# ========================================
6+
# Base class
7+
# ========================================
8+
9+
10+
class ScrollViewBase(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
super().__init__()
14+
self.views = []
15+
16+
@abstractmethod
17+
def add_view(self, view) -> None:
18+
pass
19+
20+
21+
if IS_ANDROID:
22+
# ========================================
23+
# Android class
24+
# ========================================
25+
26+
from java import jclass
27+
28+
class ScrollView(ScrollViewBase, ViewBase):
29+
def __init__(self, context) -> None:
30+
super().__init__()
31+
self.native_class = jclass("android.widget.ScrollView")
32+
self.native_instance = self.native_class(context)
33+
34+
def add_view(self, view):
35+
self.views.append(view)
36+
# In Android, ScrollView can host only one direct child
37+
if len(self.views) == 1:
38+
self.native_instance.addView(view.native_instance)
39+
else:
40+
raise Exception("ScrollView can host only one direct child")
41+
42+
else:
43+
# ========================================
44+
# iOS class
45+
# ========================================
46+
47+
from rubicon.objc import ObjCClass
48+
49+
class ScrollView(ScrollViewBase, ViewBase):
50+
def __init__(self) -> None:
51+
super().__init__()
52+
self.native_class = ObjCClass("UIScrollView")
53+
self.native_instance = self.native_class.alloc().initWithFrame_(
54+
((0, 0), (0, 0))
55+
)
56+
57+
def add_view(self, view):
58+
self.views.append(view)
59+
# Ensure view is a subview of scrollview
60+
if view.native_instance not in self.native_instance.subviews:
61+
self.native_instance.addSubview_(view.native_instance)

0 commit comments

Comments
 (0)