0

I have a fragment with SwipeRefreshLayout and NestedScrollView inside. In NestedScrollView there are several items two of which are TabLayout and ViewPager2 with two tabs. My problem is similar to this one described here but a bit more complex. Let's say if one of these two fragments, which differ in height, contains a vertical list of expandable cards, so when user interacts with these cards I want to change height of ViewPager. So this logic for changing the height of the ViewPager (based on the solutions in link above)

private fun refreshViewPager(position: Int) {
val frag = childFragmentManager.findFragmentByTag("f${position}")
frag?.view?.let {
    it.let {
            val wMeasureSpec =
                View.MeasureSpec.makeMeasureSpec(it.width, View.MeasureSpec.EXACTLY)
            val hMeasureSpec =
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        it.measure(wMeasureSpec, hMeasureSpec)

            if (vpView.layoutParams.height != it.measuredHeight) {
                vpView.layoutParams = (vpView.layoutParams)
                    .also { lp ->
                        lp.height = it.measuredHeight

                    }
            }
            vpView.invalidate()
        }

}

}

should happen not only when onPageSelected() method is called (it works fine there)

 vpView.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        refreshViewPager(position)
    }
})

but also when I click on card to expand. When it happens my toggleDetailsLiveData is updated, and since I'm observing it in fragment where my ViewPager is, I'm trying to call same method. It sometimes changes the height of ViewPager, sometimes not and there are many bugs and weird behavior. I would appreciate any suggestion

private fun observeToggleDetails() {
    viewModel.homeRepository.toggleDetailsLiveData.observe(
    viewLifecycleOwner,
         Observer { map ->
         refreshViewPager(1)
         })
} 
dandevu
  • 21
  • 4

0 Answers0