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

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

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

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

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 枚举

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

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

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()
    }
}


http://www.kler.cn/a/372525.html

相关文章:

  • MySQL 分库分表
  • jenkins自动化构建vue(web)项目并部署(项目实战)
  • 短视频矩阵系统源码开发分享/源代码部署/oem贴牌搭建分享
  • upload-labs靶场Pass-21
  • 【undefined reference to xxx】zookeeper库编译和安装 / sylar项目ubuntu20系统编译
  • 分布式光伏是什么意思?如何高效管理?
  • Windows on ARM上使用sherpa-onnx实现语音识别
  • MFC界面开发组件Xtreme Toolkit Pro v24全新发布—完整的SVG支持
  • FPGA 第一讲
  • 快速生成高质量提示词,Image to Prompt 更高效
  • 简道云和企业微信数据同步集成案例
  • Python 操作 读/写 Excel
  • 电科金仓(人大金仓)更新授权文件(致命错误: XX000: License file expired.)
  • 鸿蒙系统开发入门:一步步踏上创新之旅
  • conda使用指南
  • 100种算法【Python版】第24篇——Bellman-Ford算法
  • Java异常2
  • 计算机网络-常用网络命令和工具
  • 华为配置手工负载分担模式链路聚合实验
  • 树莓派4处理器超频指南
  • 输电线路火灾隐患监测系统功能与应用是什么?
  • 基于Springboot+微信小程序的任务打卡系统(含源码数据库)
  • 网络搜索引擎Shodan(2)
  • CSS - grid制作表格
  • Java项目实战II基于微信小程序的计算机实验室排课与查询系统(开发文档+数据库+源码)
  • 深入理解所有权与借用——借用与生命周期管理