inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[LV1] Jetpack Compose - UI 연습하기

CoupangEx - 3

HorizontalPagerIndicator 의 pagerState = pageState, 에서 빨간줄이 생깁니다

516

shafeel2

작성한 질문수 67

0

MainActivity.kt

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.getValue
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.material3.ExperimentalMaterial3Api
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPagerIndicator


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

@Composable
fun ShopEx() {
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        Column() {
            TopLogo()
            TopSearchBar()
            TopBanner()
        }
    }
}

@Composable
fun TopLogo(){
    Box(){
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(60.dp),
            contentAlignment = Alignment.Center
        ){
            Row(){
                Text("C", fontSize = 30.sp, color= Color(0xff964b00))
                Text("O", fontSize = 30.sp, color= Color(0xff964b00))
                Text("U", fontSize = 30.sp, color= Color(0xff964b00))
                Text("P", fontSize = 30.sp, color= Color.Red)
                Text("A", fontSize = 30.sp, color= Color.Yellow)
                Text("N", fontSize = 30.sp, color= Color.Green)
                Text("G", fontSize = 30.sp, color= Color.Blue)
            }
            Icon(
                imageVector = Icons.Default.ShoppingCart,      // ImageVector 아이콘지정
                contentDescription = "쇼핑",
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 20.dp)
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopSearchBar(){
    var inputText by remember {
        mutableStateOf("")
    }

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .border(1.dp, Color.Gray, shape = RoundedCornerShape(10.dp))
            .padding(start = 20.dp, end = 20.dp),
        contentAlignment = Alignment.Center
    ){
        TextField(
            value = inputText,
            onValueChange = {
                inputText = it
            },
            leadingIcon = { Icon(imageVector = Icons.Default.Search, contentDescription = null)},
            placeholder = { Text(text = "쿠팡에서 검색하세요")},
            modifier = Modifier.fillMaxWidth(),
            colors = TextFieldDefaults.textFieldColors(
                containerColor = Color.White,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            )
        )
    }
}

@OptIn(ExperimentalFoundationApi::class, ExperimentalPagerApi::class)
@Composable
fun TopBanner(){
    val pageCount = 5
    val pageState = rememberPagerState()

    val textList = listOf(
        "광고 문구1",
        "광고 문구2",
        "광고 문구3",
        "광고 문구4",
        "광고 문구5"
    )

    Box(
        modifier = Modifier.padding(top = 20.dp)
    ) {
        HorizontalPager(
            pageCount = pageCount,
            state = pageState,
            modifier = Modifier
                .fillMaxSize()
                .height(200.dp)
                .background(Color.LightGray),
        ) { page ->
            Text(
                text = textList[page],
                fontSize = 30.sp,
                fontWeight = FontWeight.ExtraBold,
            )
        }

        HorizontalPagerIndicator(
            pagerState = pageState,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(15.dp)
        )

    }

}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    ShopExTheme {
        ShopEx()
    }
}

 

build.gradle.kts

 

dependencies {

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    implementation ("com.google.accompanist:accompanist-pager:0.20.2")
    implementation ("com.google.accompanist:accompanist-pager-indicators:0.20.2")
    implementation ("com.google.accompanist:accompanist.pager.PagerState")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

}

 

 

 

 

 

android kotlin jetpack

답변 2

0

shafeel2

implementation ("com.google.accompanist:accompanist-pager:0.20.2")
implementation ("com.google.accompanist:accompanist-pager-indicators:0.20.2")

의 버젼에 따른 오류인것 같습니다

0

개복치개발자

잘 해결하셔서 다행입니다~

0

개복치개발자

pageCount -> count로 변경해보시겠어요?

 

@OptIn(ExperimentalFoundationApi::class, ExperimentalPagerApi::class)
@Composable
fun TopBanner(){
    val pageCount = 5
    val pageState = rememberPagerState()

    val textList = listOf(
        "광고 문구1",
        "광고 문구2",
        "광고 문구3",
        "광고 문구4",
        "광고 문구5"
    )

    Box(
        modifier = Modifier.padding(top = 20.dp)
    ) {

        HorizontalPager(
            count = pageCount,
            state = pageState,
            modifier = Modifier
                .fillMaxSize()
                .height(200.dp)
                .background(Color.LightGray),
        ) { page ->
            Text(
                text = textList[page],
                fontSize = 30.sp,
                fontWeight = FontWeight.ExtraBold,
            )
        }

        HorizontalPagerIndicator(
            pagerState = pageState,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(15.dp)
        )

    }

}

0

Chanpheng Hor

@OptIn(ExperimentalFoundationApi::class, ExperimentalPagerApi::class)
@Composable
fun TopBanner(){
    val pageState = rememberPagerState()

    val textList = listOf(
        "광고 문구1",
        "광고 문구2",
        "광고 문구3",
        "광고 문구4",
        "광고 문구5"
    )
    val pageCount = textList.size()

    Box(
        modifier = Modifier.padding(top = 20.dp)
    ) {

        HorizontalPager(
            count = pageCount,
            state = pageState,
            modifier = Modifier
                .fillMaxSize()
                .height(200.dp)
                .background(Color.LightGray),
        ) { page ->
            Text(
                text = textList[page],
                fontSize = 30.sp,
                fontWeight = FontWeight.ExtraBold,
            )
        }

        HorizontalPagerIndicator(
            pagerState = pageState,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(15.dp)
        )

    }

}

0

개복치개발자

혹시 질문주신것일까요?

6강에 비디오가 이상해요

0

25

1

메인액티비티의 내용이 강의와 다른 것 같습니다.

0

153

2

TextField에서 Cursor의 두께를 조절하는 방법이 있을까요?

0

208

2

TextField에서 테두리 제거하는 방법이 궁금합니다.

0

220

2

강의자료 링크에 접근이 되지 않습니다.

0

268

1

@OptIn(ExperimentalMaterial3Api::class) 질문

0

572

2

Jetpack Compose의 화면구성

1

509

1

프로젝스 생성 시 EmptyComposeActivity를 만드는 건가요?

0

471

1

CoupangEx 4 강에서

0

303

2

colorList 에서 빨간줄이 ...

0

230

2

Canvas drawCircle 에서

0

419

5

Canvas drawCircle 에서

0

198

1

이력서앱의 화면이 강사님과는 너무 달라서 어찌 ???

0

911

1

TopAppBar 관련부분에 빨간줄이 ..

0

381

3

Retrofit 관련 url 주소를 적을 때 주의사항입니다

0

302

1

Retrofit 관련 Logcat 화면에 아래 예외가 출력

0

624

6

Modifier.weight의 토탈

0

258

1

에뮬에 오류가 뜨네요(WebView)

0

295

2

강의자료실 주소를 알고싶어요

0

435

1

implementation 'androidx.navigation:navigation-compose:2.7.4' 입력시 발생하는 에러 문의합니다.

0

328

1

horizontalArrangement verticalAlignment 관련질문입니다

0

283

3

혹시 Lv2 강의는 언제쯤 올라오는 알수잇을까요?

0

314

2

강사님 닉네임에 생각나는 것이 있어용 ^^

0

239

1

웹뷰 화면 깨짐 현상에 대해 질문드립니다!

0

2014

2