Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 55204ae

Browse files
authored
make main ui cycle through composable (#245)
1 parent ae3df59 commit 55204ae

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-13
lines changed
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
11
package com.example.constraintlayout
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
5-
import androidx.activity.compose.setContent
6-
import androidx.constraintlayout.compose.ScreenExample6
4+
import android.view.View
5+
import android.widget.FrameLayout
6+
import android.widget.TextView
7+
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.compose.ui.platform.ComposeView
9+
import androidx.constraintlayout.compose.*
710

811
class MainActivity : AppCompatActivity() {
12+
private var mFrameLayout: FrameLayout? = null
13+
private var composeNum = 6
14+
private var MAX = 12
15+
16+
private fun show(com: ComposeView) {
17+
com.setContent() {
18+
when (composeNum) {
19+
0 -> ScreenExample()
20+
1 -> ScreenExample()
21+
2 -> ScreenExample2()
22+
3 -> ScreenExample3()
23+
4 -> ScreenExample4()
24+
5 -> ScreenExample5()
25+
6 -> ScreenExample6()
26+
7 -> ScreenExample7()
27+
8 -> ScreenExample8()
28+
9 -> ScreenExample9()
29+
10 -> ScreenExample10()
30+
11 -> ScreenExample11()
31+
}
32+
}
33+
}
34+
935
override fun onCreate(savedInstanceState: Bundle?) {
1036
super.onCreate(savedInstanceState)
11-
setContent {
12-
ScreenExample6()
37+
setContentView(R.layout.activity_main)
38+
mFrameLayout = findViewById<FrameLayout>(R.id.frame)
39+
setCompose();
40+
}
41+
42+
fun setCompose() {
43+
if (mFrameLayout!!.childCount > 0) {
44+
mFrameLayout!!.removeAllViews()
1345
}
46+
title = " example " + composeNum;
47+
findViewById<TextView>(R.id.layoutName).text = " example " + composeNum;
48+
var com = ComposeView(this);
49+
mFrameLayout!!.addView(com)
50+
show(com)
51+
}
52+
53+
fun prev(view: View) {
54+
composeNum = (composeNum + MAX - 1) % MAX
55+
setCompose()
56+
}
57+
58+
fun next(view: View) {
59+
composeNum = (composeNum + 1) % MAX
60+
setCompose();
1461
}
15-
}
62+
}

projects/ComposeConstraintLayout/app/src/main/res/layout/activity_main.xml

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,55 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="wrap_content"
6+
android:layout_height="match_parent"
7+
app:layout_optimizationLevel="none"
8+
android:background="#D9A"
79
tools:context=".MainActivity">
810

9-
<TextView
11+
<FrameLayout
12+
android:id="@+id/frame"
13+
android:layout_width="0dp"
14+
android:layout_height="0dp"
15+
android:layout_marginBottom="2dp"
16+
android:background="#0F0"
17+
app:layout_constraintEnd_toEndOf="parent"
18+
app:layout_constraintStart_toStartOf="parent"
19+
app:layout_constraintTop_toTopOf="parent"
20+
app:layout_constraintBottom_toTopOf="@+id/prev">
21+
22+
23+
</FrameLayout>
24+
25+
<Button
26+
android:id="@+id/prev"
1027
android:layout_width="wrap_content"
1128
android:layout_height="wrap_content"
12-
android:background="#00BCD4"
13-
android:text="Hello World!"
29+
android:text="prev"
30+
android:layout_marginStart="8dp"
31+
app:layout_constraintBottom_toBottomOf="parent"
32+
app:layout_constraintStart_toStartOf="parent"
33+
android:onClick="prev" />
34+
35+
<Button
36+
android:id="@+id/next"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:text="next"
40+
android:layout_marginEnd="8dp"
1441
app:layout_constraintBottom_toBottomOf="parent"
15-
app:layout_constraintLeft_toLeftOf="parent"
16-
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent" />
42+
app:layout_constraintEnd_toEndOf="parent"
43+
android:onClick="next" />
44+
45+
<TextView
46+
android:id="@+id/layoutName"
47+
android:layout_width="0dp"
48+
android:layout_height="wrap_content"
49+
android:text="Hello World!"
50+
android:gravity="center"
51+
app:layout_constraintBottom_toBottomOf="@+id/prev"
52+
app:layout_constraintStart_toEndOf="@+id/prev"
53+
app:layout_constraintEnd_toStartOf="@+id/next"
54+
app:layout_constraintTop_toTopOf="@+id/prev" />
55+
1856

1957
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)