Create a "Snapping" Horizontal Scroll View in android

Hi readers,
Today I present an Android horizontally scrolling view that snaps to child pages neatly and supports nested vertically scrolling child views.
ViewPager in Android API is a convenient component to switch screens. But if you would like to create your own component for this, it's not very hard.

This SnappingHorizontalScrollView properties:
- Swipe to next or previous screen like ViewPager.
- Containing OnScreenSwitchListener to handle event when pages is switched.
- Can add directly many child views to it.
- Can add a vertical ScrollView in each page to scroll vertically.
This is full source code (for more details, I commented in code):
And now, define a demo layout to use it! activity_main.xml:
In MainActivity, there are some important methods:
- Inflate layout from xml (item_layout.xml) and adding layouts to horizontal scroll view (inflateLayoutsToHorizontalView(ViewGroup layout) method).
- Set custom external font for TextView (setFontText() method).
Full MainActivity code:
This is item_layout.xml:
Result screens (click for full size):
pic name pic name pic name

Final thoughts

In almost cases, ViewPager is better approach. If you want to experienced how to create a custom component, you can use it! :)

My references

Share


Previous post
« Prev Post
Next post
Next Post »