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

效果,页面切换

项目结构

TabBar.kt

kotlin 复制代码
package com.weimin.strollerapp.components.tabbar

import 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 == currentDestination
            val color = if (selected)
                MaterialTheme.colorScheme.primary
            else
                MaterialTheme.colorScheme.onSurface
            Column(
                //  水平居中
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .weight(1f)

                    .clickable() { onNavigateTo(index) }
            ) {
                Image(
                    painter = painterResource(
                        id = if (selected)
                            destination.selectedIcon
                        else
                            destination.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 枚举

kotlin 复制代码
package com.weimin.strollerapp.components.tabbar
import com.weimin.strollerapp.R

enum 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 主路由

kotlin 复制代码
package com.weimin.strollerapp.components.route

import 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 主页面应用

kotlin 复制代码
package com.weimin.strollerapp

import 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.StrollerAppTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

            val navController = rememberNavController()

            StrollerAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    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()
    }
}
相关推荐
专业程序开发源10 小时前
springboot简历管理系统81389-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计
专业程序开发源10 小时前
springboot社区养老系统44071-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·php·课程设计
码艺-Alimjan11 小时前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
星光开发者12 小时前
基于决策树随机森林算法的小说推荐系统的设计与实现-计算机毕设【课程设计】78485
vue.js·算法·微信小程序·django·php·课程设计·express
vx_Biye_Design13 小时前
springboot高校选课系统82776-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·课程设计·express
niyongsheng13 小时前
后端零改动,给若依换一套现代化前端
vue.js·开源·node.js
五彩小白15 小时前
自回归和上下文
android
用户91775337187516 小时前
从零封装一个地图组件库:OpenLayers + Vue 的工程化实践
vue.js
雪芽蓝域zzs17 小时前
第7节:多选模式、自定义选项渲染、受控弹窗显隐
vue.js·elementui
事圆则缓17 小时前
Android 多渠道打包实战:Product Flavors、签名、AAB 与发包校验
android