-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
331a5d9
commit 514e2b3
Showing
6 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_23/LESSON_23.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_23 | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringArrayResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import com.example.jetpack_compose_all_in_one.R | ||
import com.example.jetpack_compose_all_in_one.ui.theme.dp_15 | ||
import com.example.jetpack_compose_all_in_one.utils.LogicPager | ||
import com.example.jetpack_compose_all_in_one.utils.PagedLessonHeader | ||
|
||
@Composable | ||
@Preview(showBackground = true) | ||
fun Lesson_23() { | ||
LessonContent() | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
private fun LessonContent() { | ||
val currentPage = rememberSaveable { mutableIntStateOf(0) } | ||
|
||
LogicPager( | ||
pageCount = 2, | ||
currentPage = currentPage | ||
) { | ||
Column( | ||
Modifier | ||
.fillMaxSize() | ||
.padding(it) | ||
) { | ||
PagedLessonHeader( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(dp_15), | ||
currentPage = currentPage.intValue, | ||
headers = stringArrayResource(R.array.lesson_23_header_text).toList(), | ||
infoContent = listOf( | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"https://www.google.com" | ||
) | ||
) | ||
|
||
when (currentPage.intValue) { | ||
0 -> TabScreen() | ||
1 -> TabScreen() | ||
} | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_23/TabScreen.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_23 | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Tab | ||
import androidx.compose.material3.TabRow | ||
import androidx.compose.material3.TabRowDefaults | ||
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
|
||
@ExperimentalMaterial3Api | ||
@Composable | ||
fun TabScreen() { | ||
var selectedTabIndex by remember { mutableStateOf(0) } | ||
|
||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { Text("Tab Demo") } | ||
) | ||
}, | ||
content = { padding -> | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(padding), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center | ||
) { | ||
Tabs( | ||
titles = listOf("Tab 1", "Tab 2", "Tab 3"), | ||
selectedTabIndex = selectedTabIndex, | ||
onTabSelected = { index -> | ||
selectedTabIndex = index | ||
} | ||
) | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Text(text = "Content for Tab ${selectedTabIndex + 1}") | ||
} | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
fun Tabs( | ||
titles: List<String>, | ||
selectedTabIndex: Int, | ||
onTabSelected: (Int) -> Unit | ||
) { | ||
TabRow( | ||
selectedTabIndex = selectedTabIndex, | ||
indicator = { tabPositions -> | ||
TabRowDefaults.Indicator( | ||
modifier = Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex]), | ||
color = Color.Black | ||
) | ||
} | ||
) { | ||
titles.forEachIndexed { index, title -> | ||
Tab( | ||
text = { Text(title) }, | ||
selected = selectedTabIndex == index, | ||
onClick = { onTabSelected(index) } | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
@ExperimentalMaterial3Api | ||
fun DefaultPreview() { | ||
TabScreen() | ||
} | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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