question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapView in a ViewPager don't scroll horizontally

See original GitHub issue

Issue Type

[x] Bug [x] Improvement

Description

A MapView in a ViewPager can’t scroll horizontally because the ViewPager intercept the scroll gesture and swipe the views. Note that vertical scrolling works fine.

After searching around a solution was found by adding two lines in the onTouchEvent() method:

@Override
public boolean onTouchEvent(MotionEvent event) {
	// Prevent  the ViewPager to swipe its views
	// See https://stackoverflow.com/questions/8594361/horizontal-scroll-view-inside-viewpager
	if (event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null) {
		getParent().requestDisallowInterceptTouchEvent(true);
	}
	return false;
} 

This could be enhanced by detecting (in an ACTION_DOWN) that the user initiate the scroll near the left or right border and then not calling requestDisallowInterceptTouchEvent(true) in that case.

This was suggested here

My 2 cents Yves

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
ycuicuicommented, Nov 24, 2017

I use 5% of map width for left limit and 95% for right limit

Sorry just saw i made a mistake in the above code. Should be read as:

// true if we intercep MOVE events in order to prevent the view pager to swipe views
private boolean intercepMove = false;

@Override
public boolean onTouchEvent(MotionEvent event) {
	// Prevent  the ViewPager to swipe instead of scrolling
	// See https://stackoverflow.com/questions/8594361/horizontal-scroll-view-inside-viewpager
	// Touching the borders allow the view pager to swipe
	switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// See if we touch the screen borders
			intercepMove = 100 * event.getX() > 5 * getWidth() && 100 * event.getX() < 95 * getWidth();
			break;
		case MotionEvent.ACTION_MOVE:
			if (intercepMove && getParent() != null) {
				getParent().requestDisallowInterceptTouchEvent(true);
			}
	}
	return false;
}

Sorry.

0reactions
spyhunter99commented, Dec 29, 2017

looks great! i’ll add it to the wiki

Read more comments on GitHub >

github_iconTop Results From Across the Web

gesture issue with mapview in viewpager page - Stack Overflow
My problem is that if the user is on the map page and uses a horizontal slide gesture, the page slides to the...
Read more >
Manage touch events in a ViewGroup - Android Developers
MyViewGroup contains multiple child views. If you drag your finger across a child view horizontally, the child view should no longer get touch ......
Read more >
Bug: Black rectangle background on scroll [35822196]
When I do the scroll of ViewPager, or do a vertical Scroll (scrollBy) in the fragment, the Map leaves behind a black rectangle,...
Read more >
Googlemap Does Not Load Inside Viewpager - ADocLib
The listview has its own scrolling rules that become difficult to deal with when you try ... when you think that the ViewPager...
Read more >
The Making of Prixing #2: Swiping the Fly-in App Menu
In Prixing, the main problem we had was to imbricate horizontally scrollable components ( ViewPager , MapView , etc.) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found