Skip to content

Commit 4a7e412

Browse files
committed
Add more Chaquopy Android components
1 parent be18881 commit 4a7e412

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

apps/android_pythonnative_3/app/src/main/java/com/pythonnative/pythonnative/MainActivity.kt

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.widget.ImageView
99
import android.widget.TextView
1010
import android.graphics.Color
1111
import androidx.constraintlayout.widget.ConstraintLayout
12+
import androidx.recyclerview.widget.RecyclerView
1213
import com.chaquo.python.PyException
1314
import com.chaquo.python.Python
1415
import com.chaquo.python.android.AndroidPlatform
@@ -22,50 +23,21 @@ class MainActivity : AppCompatActivity() {
2223
override fun onCreate(savedInstanceState: Bundle?) {
2324
super.onCreate(savedInstanceState)
2425
setContentView(R.layout.activity_main)
26+
val layoutMain = findViewById<ConstraintLayout>(R.id.layout_main)
2527

2628
// Initialize Chaquopy
2729
if (!Python.isStarted()) {
2830
Python.start(AndroidPlatform(this))
2931
}
3032
val py = Python.getInstance()
31-
val createButtonModule = py.getModule("create_button")
32-
val pyButton = createButtonModule.callAttr("create_button", this).toJava(Button::class.java)
33-
val layoutMain = findViewById<ConstraintLayout>(R.id.layout_main)
34-
layoutMain.addView(pyButton)
3533

36-
// TODO: Run python module and get button
34+
// val createButtonModule = py.getModule("create_button")
35+
// val pyButton = createButtonModule.callAttr("create_button", this).toJava(Button::class.java)
36+
// layoutMain.addView(pyButton)
3737

38-
// Generate UI from Python
39-
// val uiLayoutModule = py.getModule("ui_layout")
40-
// val layoutJson = uiLayoutModule.callAttr("generate_layout").toString()
41-
// val layout = JSONObject(layoutJson)
42-
// val widgets = layout.getJSONArray("widgets")
43-
//
44-
// for (i in 0 until widgets.length()) {
45-
// val widget = widgets.getJSONObject(i)
46-
// when (widget.getString("type")) {
47-
// "Button" -> {
48-
// val button = Button(this)
49-
// button.text = widget.getJSONObject("properties").getString("text")
50-
// button.setTextColor(Color.parseColor(widget.getJSONObject("properties").getString("textColor")))
51-
// button.setBackgroundColor(Color.parseColor(widget.getJSONObject("properties").getString("backgroundColor")))
52-
//
53-
// if (widget.has("eventHandlers") && widget.getJSONObject("eventHandlers").has("onClick")) {
54-
// val onClickFunctionName = widget.getJSONObject("eventHandlers").getString("onClick")
55-
// val onClickFunction = py.getModule("ui_layout").get(onClickFunctionName)
56-
//
57-
// button.setOnClickListener {
58-
// onClickFunction?.call()
59-
// }
60-
// }
61-
//
62-
// // Add button to your layout here
63-
// val layoutMain = findViewById<ConstraintLayout>(R.id.layout_main)
64-
// layoutMain.addView(button)
65-
// }
66-
// // Handle other widget types...
67-
// }
68-
// }
38+
val createRecyclerViewModule = py.getModule("create_recycler_view")
39+
val pyRecyclerView = createRecyclerViewModule.callAttr("create_recycler_view", this).toJava(RecyclerView::class.java)
40+
layoutMain.addView(pyRecyclerView)
6941

7042
// Existing code for displaying plot
7143
// val imageView = findViewById<ImageView>(R.id.image_home)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from java import cast, chaquopy, dynamic_proxy, jarray, jclass, static_proxy
2+
from androidx.recyclerview.widget import LinearLayoutManager
3+
from android.view import LayoutInflater
4+
from android.widget import TextView
5+
from android.content import Context
6+
7+
8+
class MyAdapter(static_proxy("androidx.recyclerview.widget.RecyclerView$Adapter")):
9+
def __init__(self, context, items):
10+
self.context = context
11+
self.items = items
12+
13+
def onCreateViewHolder(self, parent, viewType):
14+
inflater = self.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
15+
view = inflater.inflate(android.R.layout.simple_list_item_1, parent, False)
16+
return MyViewHolder(view)
17+
18+
def onBindViewHolder(self, holder, position):
19+
item = self.items[position]
20+
holder.textView.setText(item)
21+
22+
def getItemCount(self):
23+
return len(self.items)
24+
25+
26+
class MyViewHolder(dynamic_proxy("androidx.recyclerview.widget.RecyclerView$ViewHolder")):
27+
def __init__(self, itemView):
28+
super().__init__(itemView)
29+
self.textView = cast(TextView, itemView.findViewById(android.R.id.text1))
30+
31+
32+
def create_recycler_view(context):
33+
# Create RecyclerView
34+
RecyclerView = dynamic_proxy("androidx.recyclerview.widget.RecyclerView")
35+
recyclerView = RecyclerView(context)
36+
# Create a layout manager for RecyclerView
37+
layoutManager = LinearLayoutManager(context)
38+
recyclerView.setLayoutManager(layoutManager)
39+
# Create an adapter for RecyclerView
40+
items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
41+
adapter = MyAdapter(context, items)
42+
# Set the adapter on RecyclerView
43+
recyclerView.setAdapter(adapter)
44+
return recyclerView

0 commit comments

Comments
 (0)