I want to achieve Snapping effect in HorizontalScrollView
i.e when the user scrolls horizontally the item which is most visible (item visible > 50%) comes to the center.
I tried to do this using:
hsv.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollX = hsv.getScrollX(); // For HorizontalScrollView
Log.e("scrollX",String.valueOf(scrollX));
// DO SOMETHING WITH THE SCROLL COORDINATES
}
}
);
But the value is not constant even when we do not touch the screen.
Here is some part of logcat:
03-28 11:11:22.116 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.133 26639-26639/package_name E/scrollX: 792
03-28 11:11:22.133 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.151 26639-26639/package_name E/scrollX: 795
03-28 11:11:22.151 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.166 26639-26639/package_name E/scrollX: 799
03-28 11:11:22.166 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.183 26639-26639/package_name E/scrollX: 801
03-28 11:11:22.183 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.199 26639-26639/package_name E/scrollX: 803
03-28 11:11:22.199 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.216 26639-26639/package_name E/scrollX: 804
03-28 11:11:22.216 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.233 26639-26639/package_name E/scrollX: 805
03-28 11:11:22.233 26639-26639/package_name E/scrollX: 0
03-28 11:11:22.249 26639-26639/package_name E/scrollX: 806
03-28 11:11:22.249 26639-26639/package_name E/scrollX: 0
I've already tried these solutions, either I am not getting the point
or I don't know to do it
:
- HorizontalScrollView within ScrollView Touch Handling
- HorizontalScrollView with snapping effect
- Creating Custom Horizontal Scroll View With Snap or paging
- Creating a “Snapping” Horizontal Scroll View
My Usecase:
I have an HorizontalScrollView
which is attached to the adapter of Recyclerview(Vertical) so snapHelper
can be done in vertical but I don't know how to make it for horizontal.