The important think is scroll gesture. When we scroll vertically or horizontally, viewpager never knows what is happening. Because, scroll gestures are handled by scrollview. So, if we can steal scroll event from the scrollview, we can inform viewpager about scrolling.
To steal scroll event from the scrollview, we have to create our custom scrollview and create our gesture detector. And in our gesture detector, we have to check gesture direction. If gesture direction is vertical, release gesture to be handled by scrollview. If gesture direction is horizontal, prevent the event to be handled by viewpager.
public class CustomScrollView extends ScrollView { private GestureDetector mGestureDetector; public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new YScrollDetector()); setFadingEdgeLength(0); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } // Return false if we're scrolling in the x direction class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return (Math.abs(distanceY) > Math.abs(distanceX)); } }
That's all. Put your viewpager into this custom scrollview in layout.
<com.muratonnet.sample.widget.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > ... <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="250dp" /> ... </LinearLayout> </com.muratonnet.sample.widget.CustomScrollView>
You can find source code and sample usage on
13 comments:
Hello Mr.Murat,
I want to thank you for your great tip, that help me a lot thank to you.
Bye
Not Working any more..........
Not working.
Hi.
How can we make this code work for "ScrollView inside a page of ViewPager"?
Your Tutorial is too good but how can i set dynamic height if viewpager have greater height then screen size?
it is not working in bellow lolipop.
Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chenai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery
stunning, incredible, I was thinking about how to fix skin inflammation normally.I've bookmark your site and furthermore include rss. keep us refreshed.
data scientist malaysia
Simply the manner in which I have anticipated. Your site truly is intriguing.
iot courses in delhi
Here at this site actually the particular material assortment with the goal that everyone can appreciate a great deal.
360DigiTMG big data course in malaysia
I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…
masters in artificial intelligence
This is a very nice one and gives in-depth information. I am really happy with the quality and presentation of the article.
JAVA classes in Akola
I am impressed by the information that you have on this blog. It shows how well you understand this subject.
data scientist training in malaysia
Post a Comment