From 5248ce6cf86e7839c4e841a5eeb1414838dee2f1 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sun, 23 Jul 2023 22:39:44 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E5=8A=A8=E6=80=81=E9=A1=B5=20Tab=20?= =?UTF-8?q?=E6=96=87=E5=AD=97=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/ui/page/main/explore/ExplorePage.kt | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/explore/ExplorePage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/explore/ExplorePage.kt index e9d05b95..77e0082e 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/explore/ExplorePage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/explore/ExplorePage.kt @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.width import androidx.compose.foundation.pager.PagerState import androidx.compose.foundation.pager.rememberPagerState +import androidx.compose.material.MaterialTheme import androidx.compose.material.Scaffold import androidx.compose.material.Tab import androidx.compose.material.TabRow @@ -21,7 +22,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.compose.ui.util.fastForEachIndexed import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.activities.NewSearchActivity import com.huanchengfly.tieba.post.arch.GlobalEvent @@ -46,7 +51,7 @@ import kotlinx.coroutines.launch @Immutable data class ExplorePageItem( val id: String, - val name: @Composable () -> Unit, + val name: @Composable (selected: Boolean) -> Unit, val content: @Composable () -> Unit, ) @@ -73,14 +78,14 @@ private fun ColumnScope.ExplorePageTab( .align(Alignment.CenterHorizontally) .width(76.dp * pages.size), ) { - pages.forEachIndexed { index, item -> + pages.fastForEachIndexed { index, item -> Tab( - text = item.name, + text = { item.name(pagerState.currentPage == index) }, selected = pagerState.currentPage == index, onClick = { coroutineScope.launch { if (pagerState.currentPage == index) { - coroutineScope.emitGlobalEvent(GlobalEvent.Refresh(item.id)) + emitGlobalEvent(GlobalEvent.Refresh(item.id)) } else { pagerState.animateScrollToPage(index) } @@ -91,6 +96,19 @@ private fun ColumnScope.ExplorePageTab( } } +@Composable +private fun TabText( + text: String, + selected: Boolean +) { + val style = MaterialTheme.typography.button.copy( + letterSpacing = 0.75.sp, + fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal, + textAlign = TextAlign.Center + ) + Text(text = text, style = style) +} + @OptIn(ExperimentalFoundationApi::class) @Composable fun ExplorePage() { @@ -103,17 +121,17 @@ fun ExplorePage() { listOfNotNull( if (loggedIn) ExplorePageItem( "concern", - { Text(text = stringResource(id = R.string.title_concern)) }, + { TabText(text = stringResource(id = R.string.title_concern), selected = it) }, { ConcernPage() } ) else null, ExplorePageItem( "personalized", - { Text(text = stringResource(id = R.string.title_personalized)) }, + { TabText(text = stringResource(id = R.string.title_personalized), selected = it) }, { PersonalizedPage() } ), ExplorePageItem( "hot", - { Text(text = stringResource(id = R.string.title_hot)) }, + { TabText(text = stringResource(id = R.string.title_hot), selected = it) }, { HotPage() } ), ).toImmutableList()