On a recent Android project I spent 1.5 days tearing my hair out over an issue that ended up being a one-line fix.
The app has a
ViewPager which hosts one or more
As you can see each “tile” is a handful of
TextView objects along with an
ImageView for the background.
The issue is sometimes when the user swipes to scroll through pages the
ViewPager would not handle the touch/swipe event and nothing would happen. I know the
ViewPager was receiving the touch event because I could over-ride
onTouchEvent and log the event.
It made no sense. And the issue was sporadic to make matters even worse.
Without burying the lede too much the problem was that my
TextView (the one that says “401.75” or “7 days” for instance had `android:singleLine=“true” and it was the culprit.
Basically, the case is: if the view can scroll horizontally, it intercepts the horizontal motion event and ViewPager is not able to process it anymore.
Since API Level 14 TextViews have
android:scrollHorizontally property (and
setHorizontallyScrolling(boolean) method), which, if set to true, causes the TextView to intercept horizontal scroll motion events.
You may set it to false either in XML or right in the code, but watch out:
android:singleLine property forces
android:scrollHorizontally to be set to true! Very tricky point! But fortunately, you usually able to safely replace single line property with
android:maxLines="1" and necessary ellipsize value.
Since I didnt really need
singleLine I removed it and voila, ViewPager was swiping again!
So moral of the story: watch out for