google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.43k stars 598 forks source link

[Pager Indicators] HorizontalPagerIndicator, PagerState param type mismatch #1559

Closed es0329 closed 1 year ago

es0329 commented 1 year ago

Description Type mismatch at pagerState param when attempting to use HorizontalPagerIndicator.

Steps to reproduce

  1. Begin with a simple implementation of the now-deprecated Accompanist HorizontalPager and HorizontalPagerIndicator.
  2. Follow the Pager migration guidance.
  3. Reference the new rememberPagerState() to pass a androidx.compose.foundation.pager.PagerState param into Accompanist's HorizontalPagerIndicator.
  4. Compilation error.
    > Task :app:compileDevDebugKotlin FAILED
    e: Type mismatch: inferred type is androidx.compose.foundation.pager.PagerState but com.google.accompanist.pager.PagerState was expected

Expected behavior Overloaded support for AndroidX PagerState according to migration table.

Additional context androidx.compose.foundation:foundation:1.4.0 com.google.accompanist:accompanist-pager-indicators:0.30.0

Screenshot 2023-03-24 at 10 36 20 PM
bentrengrove commented 1 year ago

Just a guess while I triage this issue, try deleting any import for HorizontalPagerIndictor and also I think you have to include a pageCount in the foundation compatible version

es0329 commented 1 year ago

Thanks @bentrengrove, these items were completed. The param pageCount was present since my prior (Accompanist) implementation, and the indicator was brought in via import com.google.accompanist.pager.HorizontalPagerIndicator.

kostascollctiv commented 1 year ago

Just a guess while I triage this issue, try deleting any import for HorizontalPagerIndictor and also I think you have to include a pageCount in the foundation compatible version

Can confirm this worked. I've deleted the import of the HorizontalPagerIndictor and the HorizontalPagerIndictor function, then the new variation appeared.

gaohomway commented 1 year ago

I don't understand, how to solve this problem of inconsistent state types?

Required:com.google.accompanist.pager.PagerState

Found:androidx.compose.foundation.pager.PagerState

kostascollctiv commented 1 year ago

I don't understand, how to solve this problem of inconsistent state types?

Required:com.google.accompanist.pager.PagerState

Found:androidx.compose.foundation.pager.PagerState

  1. Delete the accompanist import
  2. Delete HorizontalPager from your code
  3. Try typing HorizontalPager and the autofill should give you 2 options, one of them takes androidx.compose.foundation.pager.PagerState as a pager type (hint: it's the one that doesn't have a strikethrough)
es0329 commented 1 year ago

My apologies @bentrengrove, I misunderstood your guidance. It was my HorizontalPager that had a pre-existing pageCount param, but you were directing me to also add that param to HorizontalPagerIndicator.

Adding that newly required param to HorizontalPagerIndicator does indeed resolve its overloaded signature, which works with the Compose PagerState. Closing this issue; thank you for taking a look. 🙌🏽

gaohomway commented 1 year ago

@kostascollctiv What I need to address is the HorizontalPagerIndicator

截屏2023-03-29 12 45 52
kostascollctiv commented 1 year ago

@kostascollctiv What I need to address is the HorizontalPagerIndicator

截屏2023-03-29 12 45 52

Ah, apologies! The solution is the same but replace HorizontalPager with HorizontalPagerIndicator. Screenshot 2023-03-29 at 11 13 23

es0329 commented 1 year ago

@gaohomway We had the same mistake. HorizontalPagerIndicator now needs a pageCount param for the overload we want to resolve. This differs slightly from the prior way you and I had done it.

The legacy method defaults pageCount by accessing pagerState.pageCount. The new method worked for me after I explicitly supply the pageCount param.

HorizontalPagerIndicator(
    pagerState = pagerState,
    pageCount = viewModel.data.size,
    modifier = Modifier.padding(0.dp, 8.dp)
)
gaohomway commented 1 year ago

@es0329 You are right. I have solved this problem. Thank you for replying to me. Thank you also to @kostascollctiv