当前位置: 首页 > news >正文

JetPack Compose安卓开发之底部导航Tabbar

效果,页面切换
在这里插入图片描述

项目结构
在这里插入图片描述
TabBar.kt

package com.weimin.strollerapp.components.tabbarimport androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp@Composable
fun TabBar(destinations: List<Navigation>,currentDestination: String,onNavigateTo: (Int) -> Unit,modifier: Modifier = Modifier
): Unit {Row(// 底部导航栏宽度充满屏幕,并远适配离安全区底部modifier = Modifier.fillMaxWidth().navigationBarsPadding()) {destinations.forEachIndexed { index, destination ->val selected = destination.route == currentDestinationval color = if (selected)MaterialTheme.colorScheme.primaryelseMaterialTheme.colorScheme.onSurfaceColumn(//  水平居中horizontalAlignment = Alignment.CenterHorizontally,modifier = Modifier.weight(1f).clickable() { onNavigateTo(index) }) {Image(painter = painterResource(id = if (selected)destination.selectedIconelsedestination.unSelectedIcon),contentDescription = stringResource(id = destination.titleTextId),modifier = Modifier.size(25.dp))Spacer(modifier = Modifier.width(5.dp))Text(text = stringResource(id = destination.titleTextId), color = color)}}}
}

Navigation.kt 枚举

package com.weimin.strollerapp.components.tabbar
import com.weimin.strollerapp.Renum class Navigation(val selectedIcon: Int,val unSelectedIcon: Int,val titleTextId: Int,val route: String,
) {HomePage(selectedIcon = R.drawable.home1,unSelectedIcon = R.drawable.home,titleTextId = R.string.tabbar_home,route = "Home_Page"),MinePage(selectedIcon = R.drawable.home1,unSelectedIcon = R.drawable.home,titleTextId = R.string.tabbar_mine,route = "Mine_Page")
}

MainRoute.kt 主路由

package com.weimin.strollerapp.components.routeimport androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import com.weimin.strollerapp.components.tabbar.Navigation
import com.weimin.strollerapp.components.tabbar.TabBar
import com.weimin.strollerapp.view.home.HomePage
import com.weimin.strollerapp.view.mine.MinePage
import kotlinx.coroutines.launch@Composable
fun MainRoute(finishPage: () -> Unit = {}) {MainScreen()
}//配置导航
@Composable
fun MainScreen(finishPage: () -> Unit = {}
) {//x当前选中的界面名称var currentDestination by rememberSaveable {mutableStateOf("Home_Page")}// 显示页面,控制页面val pagerState = rememberPagerState {4}val scope = rememberCoroutineScope()Column(modifier = Modifier.fillMaxSize()) {HorizontalPager(state = pagerState,userScrollEnabled = true,
//            beyondBoundsPageCount = 4, //加载屏幕外的更多页面modifier = Modifier.fillMaxWidth().weight(1f)  // 占满剩余空间(高度)) { page ->when (page) {0 -> HomePage()1 -> MinePage()}}TabBar(destinations = Navigation.entries,currentDestination = currentDestination,onNavigateTo = { index ->currentDestination = Navigation.values()[index].route// 启动协程scope.launch {//   挂起函数pagerState.animateScrollToPage(index)}},modifier = Modifier)}
}

MainActivity.kt 主页面应用

package com.weimin.strollerappimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.rememberNavController
import com.weimin.strollerapp.components.route.MainRoute
import com.weimin.strollerapp.ui.theme.StrollerAppThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {val navController = rememberNavController()StrollerAppTheme {// A surface container using the 'background' color from the themeSurface(modifier = Modifier.fillMaxSize(),color = MaterialTheme.colorScheme.background) {Greeting(
//                        navController= navController)}}}}
}@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Greeting() {MainRoute()
}@Preview(showBackground = true)
@Composable
fun GreetingPreview() {StrollerAppTheme {Greeting()}
}

http://www.mrgr.cn/news/62129.html

相关文章:

  • 大模型入门综述---从模型,训练,部署全方面认识大模型
  • mysql 通过GROUP BY 聚合并且拼接去重另个字段
  • java 时间和字符串转换
  • 【http作业】
  • SQL PRIMARY KEY
  • C语言 | Leetcode C语言题解之第508题斐波那契数
  • 解决Selenium的3大痛点!这款工具让你的自动化测试效率翻倍!
  • 嵌入式linux跨平台基于mongoose的TCP C++类的源码
  • SQLark百灵连接——整合项目监控过程
  • 中酱集团:一日三餐吃出健康生活
  • 四、鸿蒙开发-常用布局(线性布局、层叠布局、弹性布局、网格布局、列表布局)
  • C语言导航 3.3指针运算符
  • QT:QThread 使用案例
  • 【Redis:原理、架构与应用】
  • JavaScript 实战技巧:让你成为前端高手的必备知识2
  • 【热管理】日本三洋 sanace 散热风扇
  • 阿里巴巴店铺商品API返回值中的商品分类与筛选条件
  • Docker:存储原理
  • 理解处理器寻址
  • “胖东来”进京赶考,永辉有救了?
  • 睿亚资产郭威:公益路上的坚定行者,照亮希望之光
  • 大数据治理:确保数据价值与合规性的战略框架
  • 【php常用公共函数】php获取指定时间段中有那几年并输出年份的起始时间和结束时间
  • 酱香经典——茅台镇的酱酒“四台”的传奇
  • 姚望篮坛数十秋 巨人肩头月满楼 新篇开启情难舍 篮球梦续望云头
  • Java避坑案例 - 线程池使用中的风险识别与应对