In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use ViewPager to prohibit left and right sliding in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to use ViewPager to prohibit left and right sliding in Android" can help you solve the problem.
When viewPager distributes events, it does not intercept events and hands them over to child consumption. When viewPager receives an event, its own dispatchTouchEvent must be called, and its result is affected by its own onInterceptTouchEvent. Here I will onInterceptTouchEvent-> return false, which means that the pager itself does not intercept the event, and the event will be passed to child view. At the same time, the dispatchTouchEvent of the child view will be called. If the dispatchTouchEvent-> return false of the sub-view, you can correspond to the sub-view onInterceptTouchEvent-> return true intercepts the event, but the sub-view onTouchEvent-> return false does not handle the event. Then the onTouchEvent of the pagers itself is called to consume the event. The code is as follows:
Public class NoScrollVP extends ViewPager {public NoScrollVP (Context context) {this (context, null);} public NoScrollVP (Context context,AttributeSet attrs) {super (context, attrs);} @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {return false;// does not intercept events} @ Override public boolean onTouchEvent (MotionEvent ev) {/ / linked to business requirements return true / / if the sub-view intercepts but does not handle it, you will process the}} 1234567891011121314151617pager source code yourself.
On this issue, you can refer to the source code of pager. When we slide pager around, pager will slide through the page, and when we slide up and down, pager does not deal with it. The code can be referred to:
If (mScrollState = = SCROLL_STATE_SETTLING & & Math.abs (mScroller.getFinalX ()-mScroller.getCurrX ()) > mCloseEnough) {/ / Let the user 'catch' the pager as it animates. MScroller.abortAnimation (); mPopulatePending = false; populate (); mIsBeingDragged = true; requestParentDisallowInterceptTouchEvent (true); setScrollState (SCROLL_STATE_DRAGGING);} else {completeScroll (false); mIsBeingDragged = false;} 12345678910111213
This is called in the onInterceptTouchEvent method of pager, and the mIsBeingDragged parameter is critical, which is the return value of the onInterceptTouchEvent method. When we are sliding to the end of the up, if the absolute distance of the scroll ✖️ offset > mCloseEnough, then the pager will intercept the event itself, otherwise, it will be passed down. Where mCloseEnough = 2dp * screen density (160dpi screen, density is 1X 120dpi screen, density 0.75, and so on). Of course, the actual processing logic of pager is much more complex. For example, in move, a series of judgments have been made on whether to intercept events, but the idea of event distribution is the same.
This is the end of the introduction on "how to use ViewPager to prohibit left and right sliding in Android". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.