Skip to content

Instantly share code, notes, and snippets.

@safaorhan
Created February 11, 2020 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safaorhan/1a541af729c7657426138d18b87d5bd4 to your computer and use it in GitHub Desktop.
Save safaorhan/1a541af729c7657426138d18b87d5bd4 to your computer and use it in GitHub Desktop.
Disables the child attach listener so that inflated children with wrap_content heights can pass.
/**
* Disables the child attach listener so that inflated children with wrap_content heights can pass.
*
* This is very fragile and depends on the implementation details of [ViewPager2].
*
* @see ViewPager2.enforceChildFillListener (the removed listener)
*/
private fun ViewPager2.hackMatchParentCheckInViewPager() {
(getChildAt(0) as RecyclerView).clearOnChildAttachStateChangeListeners()
}
@muth0mi
Copy link

muth0mi commented Apr 15, 2020

It doesn't seem to work for me.

@safaorhan
Copy link
Author

@muth0mi you need to do other changes as well. This is to just bypass the runtime check in the ViewPager

@AditChauhan
Copy link

what all other changes are required to achieve this.

@safaorhan
Copy link
Author

It's been a long time I came up with this. You need to be sure that your inflated children uses wrap_content height.

@AditChauhan
Copy link

AditChauhan commented Jan 17, 2021

thanks @safaorhan , it solved my problem.

@safaorhan
Copy link
Author

Glad to hear that!

@ChinGyi2019
Copy link

ChinGyi2019 commented Feb 2, 2021

Thanks this work for me ,when we want to use viewpager as Book ' pages. There are different kinds of page height. Sometimes , we need to scroll action when page is too long :3 At that time, just wrap your viewpager's item layout with NestedScroll View..

Thanks to @safaorhan

@safaorhan
Copy link
Author

@ChinGyi2019 Glad to hear that it helped!

Our use case was similar, we were showing three items with different heights in ViewPager.

I was getting a calculation of all child views to calculate the one with the biggest height. So I was dynamically changing the height of the ViewPager and triggering a second measurement round

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment