PublisherAdView is causing RecyclerView (any scrollable view) to shift?
Recently the 9GAG Android app has integrated PublisherAdView
to post list. We found a strange bug that the list( RecyclerView
) will shift when we scroll to those PublisherAdView
and perform tab changes in ViewPager
. We switch from tab 1 and then to tab 2 and then back to tab 1. The list will be shifted, it looks as if the advertisement is trying to put it to a visible position! (Smart, huh?)
Demonstration:
After some investigation, and we found this StackOverflow question.
It’s because PublisherAdView
is a WebView
under the hood, and WebView
itself will trigger a focus request. In the question, the answer is to add android:focusableInTouchMode="true"
attribute, however, this will make the LinearLayout
focusable, which doesn’t solve the issue.
Solution is simple, we used this attribute android:descendantFocusability="beforeDescendants"
to its parent view. 🎉 That solves the issue immediately! This trick should applies to any scrollable views (RecyclerView, ListView and ScrollView)
Example layout: