Course contentsShow
← React Native
Lesson 344 of 1,5554. Flexbox and Native StylingFree lesson

Sticky Item Collisions

Manage the visual transition when two stick-positioned elements meet within a scroll view.

In this lesson, you will learn how to manage the visual overlapping behavior when two sticky-positioned elements meet within a ScrollView.

When you use the stickyHeaderIndices prop on a ScrollView, you define elements that "stick" to the top of the container as the user scrolls. However, if you have multiple sticky elements—such as category headers in a long list—they will naturally collide. By default, the second sticky element will slide underneath and eventually replace the first one as it reaches the top of the screen.

Understanding the Collision

A "collision" occurs when the scroll offset causes the bottom edge of a new sticky item to push against the bottom edge of the currently pinned item. Without intervention, this results in an abrupt "jump" or a stack-up effect that can obscure content.

To manage these transitions effectively, we rely on the stickyHeaderIndices array, which tells the ScrollView exactly which children should maintain this behavior. If you want to avoid visual clutter during a collision, ensure your sticky components have a consistent height and a background color that masks the content sliding behind them.

Code Illustration

Here is how you define multiple sticky headers:

<ScrollView stickyHeaderIndices={[0, 3]}>
  <View style={styles.header}>
    <Text>Category A</Text>
  </View>
  <View><Text>Item 1</Text></View>
  <View><Text>Item 2</Text></View>
  <View style={styles.header}>
    <Text>Category B</Text>
  </View>
  <View><Text>Item 3</Text></View>
</ScrollView>

In this example, when "Category B" reaches the top, it will physically push "Category A" out of the viewport. If your UI requires a more graceful transition (like an animation or a custom header stack), you would need to use Animated.Value tied to the onScroll event to manually calculate the offset of the colliding elements.

For most layouts, simply ensuring your headers are opaque and sized consistently prevents the "clipping" look that often confuses users during high-velocity scrolling.

Key Takeaway: Sticky item collisions are handled automatically by stickyHeaderIndices, but you must use opaque background colors and consistent heights to prevent visual artifacts when headers overlap or transition.