Last active
July 14, 2022 10:25
-
-
Save Hasiy/349193c7a761d2848ea262b8f26bb94b to your computer and use it in GitHub Desktop.
NoScrollViewPager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoScrollViewPager @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ViewPager(context, attrs) { | |
private var isScroll = false | |
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { | |
// return false;//可行,不拦截事件, | |
// return true;//不行,孩子无法处理事件 | |
// return super.onInterceptTouchEvent(ev);//不行,会有细微移动 | |
return if (isScroll) { | |
super.onInterceptTouchEvent(ev) | |
} else { | |
false | |
} | |
} | |
@SuppressLint("ClickableViewAccessibility") | |
override fun onTouchEvent(ev: MotionEvent?): Boolean { | |
// return false;// 可行,不消费,传给父控件 | |
// return true;// 可行,消费,拦截事件 | |
// super.onTouchEvent(ev); //不行, | |
// 虽然onInterceptTouchEvent中拦截了, | |
// 但是如果viewpager里面子控件不是viewGroup,还是会调用这个方法. | |
return if (isScroll) { | |
super.onTouchEvent(ev) | |
} else { | |
true // 可行,消费,拦截事件 | |
} | |
} | |
fun setScroll(scroll: Boolean) { | |
isScroll = scroll | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment