3

How to properly use the "peekEnabled" property on ViewPagerAndroid ?

<ViewPagerAndroid peekEnabled={true} >{this.props.children}</ViewPagerAndroid>

I want something like this

enter image description here

Tiziano Munegato
  • 869
  • 1
  • 7
  • 21

1 Answers1

1

Seems that peekEnabled to work as expected needs ViewPagerAndroid to have padding.
Looking at the RN source code, child views style is set to position: absolute and right,left,top,bottom: 0 so the parent's container padding has no effect.

As a workaround you can set pageMargin to a negative value and use paddingLeft,paddindRight in the child views to achieve the same effect.

e.g:

<ViewPagerAndroid pageMargin={-70} style={{ flex: 1 }}>
  <View style={{ padding: 35 }}>
    <View style={{ backgroundColor: 'pink', flex: 1 }} />
  </View>
  <View style={{ padding: 35 }}>
    <View style={{ backgroundColor: 'cyan', flex: 1 }} />
  </View>
  <View style={{ padding: 35 }}>
    <View style={{ backgroundColor: 'magenta', flex: 1 }} />
  </View>
</ViewPagerAndroid>

demo

koox00
  • 2,691
  • 2
  • 15
  • 25
  • Dang that is interesting. Seems like a workaround for a bug. Thanks for sharing such tips! I would never have found thsi out! How much padding? Even just 1dpi padding? May you please link the source code you mention. If possible may you please share your own example code. – Noitidart Mar 10 '18 at 18:16
  • 1
    @Noitidart I think the values would vary, I found [this](http://www.jayrambhia.com/blog/android-viewpager-cards-1) helpful. I added a link to a working snack to demonstrate. – koox00 Mar 10 '18 at 18:45