0

I have a header which is basically a rounded rectangle with some text and two buttons and a background image behind the rectangle which stretches to the very top.

And, I have some ’TabRow’ buttons underneath the ‘Header’ which show certain webpages. I want the Header to disappear when the User scrolls down and reappear when scrolling up. But, the Header refreshes with every Tab change.

Does anyone have any idea what to do, please? I tried to change the Header to a separate file too.

Thanks in advance.

MAINACTIVITY:

@Composable
fun MyApp() {
    val tabs = listOf("Home", "Contact")
    var selectedTab by remember { mutableStateOf(0) }
    var headerVisible by remember { mutableStateOf(true) }  // Control header visibility



    val animatedAlpha by animateFloatAsState(if (headerVisible) 1f else 0f)

    Column {
        // ✅ Moved Header to a Separate Function (Prevents Refresh)
        if (animatedAlpha > 0f) {
            Header()
        }

        // Tabs
        TabRow(
            selectedTabIndex = selectedTab,
            backgroundColor = Color.White, // ✅ Background color of TabRow
            modifier = Modifier
                .fillMaxWidth()
                .offset(y = 0.dp) // ✅ Keeps it in place
                .zIndex(1f) // ✅ Ensures tabs stay above other components if needed
        ) {
            tabs.forEachIndexed { index, title ->
                Tab(
                    selected = selectedTab == index,
                    onClick = { selectedTab = index },
                    selectedContentColor = Color(0xff1f68da), // ✅ Color when selected
                    unselectedContentColor = Color.Gray, // ✅ Color when not selected
                    text = {
                        Text(
                            text = title,
                            fontFamily = customFontFamily,
                            fontWeight = FontWeight.Normal,
                            color = if (selectedTab == index) Color(0xff1f68da) else Color.Gray
                        )
                    }
                )
            }
        }


// WebView Content Based on Selected Tab
        when (selectedTab) {
            0 -> HomeView { scrollDiff -> headerVisible = scrollDiff <= 0 }
            1 -> ContactView { scrollDiff -> headerVisible = scrollDiff <= 0 }
        }
    }
}


HEADER:

fun Header() {
    Box(
        modifier = Modifier.fillMaxWidth()
    ) {
        // Background Image
        Image(
            painter = painterResource(id = R.drawable.header),
            contentDescription = "Header Background",
            modifier = Modifier
                .fillMaxWidth()
                .height(220.dp),
            contentScale = ContentScale.Crop
        )

        // White Rounded Rectangle with Shadow
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(185.dp)
                .offset(y = 70.dp)
                .shadow(8.dp, shape = RoundedCornerShape(16.dp))
                .background(Color.White, shape = RoundedCornerShape(16.dp))
                .zIndex(2f)
                .padding(10.dp)
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Spacer(modifier = Modifier.height(1.dp))
                Text(
                    text = "HEADER TEXT”,
                    fontFamily = customFontFamily,
                    fontWeight = FontWeight.Bold,
                    fontSize = 17.sp,
                    color = Color.Black,
                    modifier = Modifier.align(Alignment.Start)
                )
                Spacer(modifier = Modifier.height(3.dp))
                Text(
                    text = "Subtitle...”,
                    fontFamily = customFontFamily,
                    fontWeight = FontWeight.Normal,
                    fontSize = 15.sp,
                    color = Color.Black,
                    modifier = Modifier.align(Alignment.Start)
                )
                Spacer(modifier = Modifier.height(7.dp))


HOMEVIEW:


package com.phela.hsmapp

import androidx.compose.runtime.Composable

@Composable
fun HomeView(onScroll: (Int) -> Unit) {
    WebViewPage(url = "https://www.google.com”, onScroll = onScroll)
}


0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.