Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical scrolling using an AndroidView ViewPager #60

Open
leonardo-ndo opened this issue Aug 13, 2022 · 0 comments
Open

Vertical scrolling using an AndroidView ViewPager #60

leonardo-ndo opened this issue Aug 13, 2022 · 0 comments

Comments

@leonardo-ndo
Copy link

Hello all.

Here's my situation:

I need to implement a (not compose, so I can't use HorizontalPager) ViewPager on my screen. Each ViewPager page is a list with an infinite scroll feature implemented. I'm using the ScrollableTabRow composable for tabs.

The screen is all implemented, but I'm facing issues with the scrolling.

CollapsingToolbarScaffold lib recommends to set verticalScroll(rememberScrollState()) to the parent Column of my content. This works fine, but the ViewPager height doesn't seem to be calculated, making the scroll not happen. After some research, I found that we need to set the height explicitly if I want to use verticalScroll, but I couldn't make it work.

Here's what I've already tried:

  1. Wrap the content in BoxWithConstraints and set the maxHeight;
  2. Implement onGloballyPositioned modifier;
  3. Set scrollable(rememberScrollState(), Orientation.Vertical)
  4. Set ViewCompat.setNestedScrollingEnabled(this, true) to the ViewPager (this works, but the scroll gets no smooth at all so, certainly not an option).

Any ideas? Here's my code:

val selectedPage = remember {
        mutableStateOf(0)
    }

    val pagerState = rememberPagerState(initialPage = selectedPage.value)

    Surface(modifier = Modifier.fillMaxSize(), color = Color(0xFFF1F3F5)) {

        Column {
            ScrollableTabRow(
                edgePadding = 0.dp,
                selectedTabIndex = selectedPage.value,
                backgroundColor = Color.Transparent,
                modifier = Modifier
                    .padding(16.dp)
                    .height(36.dp),
                indicator = { },
                divider = { }
            ) {
                NativeTab("Tab 1", 0, pagerState, selectedPage)
                NativeTab("Tab 2", 1, pagerState, selectedPage)
                NativeTab("Tab 3", 2, pagerState, selectedPage)
            }

            AndroidView(
                factory = { context ->
                    ViewPager(context).also {
                        it.adapter = CustomPagerAdapter(context)
                        it.currentItem = selectedPage.value
                        it.addOnPageChangeListener(object :
                            ViewPager.OnPageChangeListener {
                            override fun onPageScrolled(
                                position: Int,
                                positionOffset: Float,
                                positionOffsetPixels: Int
                            ) {
                            }

                            override fun onPageSelected(position: Int) {
                                selectedPage.value = position
                            }

                            override fun onPageScrollStateChanged(state: Int) {
                            }
                        })
                        ViewCompat.setNestedScrollingEnabled(it, true)
                    }
                },
                update = {
                    it.currentItem = selectedPage.value
                },
                modifier = Modifier.weight(1f)
            )
        }
    }

Any help would be much appreciated.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant