Reprint Annotated Source: http://blog.csdn.net/xiaohanluo/article/details/52130923
1. Preface
In Android development, it is assumed that there are some simple layouts. are very easy to fix. But when it comes to complex pages, especially when ScrollView is used for small screen phones, there will be a lot of collisions with the click events. The most classic is the ScrollView nested in the ListView.
I think most of the students who have just started to touch Android have stepped on the pit, and this article starts with a recent project. Then provide some ideas for conflict resolution in the process.
2. Project initiation
The project has a page that involves Viewpager. Mapview. ListView, that is, in a page, there will be three view, very obvious, the screen can not be fully displayed, need ScrollView to do support, introduced the ScrollView as the outer container.
However, because this page data display need to do the user manual pull-down refresh, and then introduced the official swiperefreshlayout.
So the layout of this page is like this.
For example with (detail layout ignored).
Figure-1 layout diagram
New problems were introduced after the addition of ScrollView and Swiperefreshlayout. Is the event conflict between controls, Viewpager, Mapview, and ListView nested in ScrollView need to be able to handle click events correctly, especially the ListView. requirements require it to slide in the ScrollView, and the two slips are confused, not a particular merit.
The problem is brought forward, the following directly look at the solution of ideas.
3. The idea of solving the sliding conflict
There is a method called in ViewGroup requestDisallowInterceptTouchEvent(boolean disallowIntercept) , which can be used to control whether the ViewGroup truncates the Click event. When we solve a sliding conflict, it is actually a time to call this method. Let the parent layout not truncate the Click event and pass the Click event to Child view. Let the relevant sub-view to deal with.
Here are some examples and thoughts on dealing with various click event conflicts in your project. The method of treatment is merely to provide a way of thinking, which may not be the optimal method, there must be other ways to solve the problem.
The following scenarios for handling sliding conflicts are handled in the ontouchlistener of the child view. Does not go to the Click event handling process of the replication control. In use is still relatively convenient.
3.1 Mapview Map Page slide conflict
The main conflict between Mapview and ScrollView is when the user clicks on the Mapview map and slides. You want to handle click events from map maps. It includes a sliding event, a two-finger zoom map, and more.
In ScrollView, it is the default to truncate the Click event, causing the user to click on the map, the map is basically unresponsive. Let's not talk about zooming the map with both hands.
The user finger clicks to the map, and when sliding, it is very difficult to determine whether the user is to scrollview up and down or manipulate the map content slide, so I simply feel, just want the user finger click to the map. is to manipulate the map. When the user's finger is lifted. It feels that the user does not need to operate the map.
The solution is also very simple. is to let ScrollView not truncate the Click event when the user clicks on the map or slides the map. and passed to the child view processing. This is the map to handle the Click event, and when the user's finger is lifted, the ScrollView state is restored to its previous state. That is, ScrollView can truncate the Click event.
I use Baidu map, directly on the code, more easy to understand.
Mmapview.getchildat (0). Setontouchlistener (NewView.ontouchlistener () {@Override Public Boolean OnTouch(View V, motionevent event) {if(event.getaction () = = motionevent.action_up) {//Agree ScrollView truncate Click event, ScrollView can slideMscrollview.requestdisallowintercepttouchevent (false); }Else{//Do not agree to ScrollView truncation of click events, click events from view processingMscrollview.requestdisallowintercepttouchevent (true); }return false; } });
3.2 Viewpager Sliding conflict resolution
In this project, Viewpager is at the top of the page. Suppose that the Viewpager is nested only inside the ScrollView. Because these two controls are sliding events in different directions, there is basically no conflict.
However, due to the introduction of Swiperefreshlayout, I am now sliding viewpager. Very easy to trigger the swiperefreshlayout down the refresh, which may have blocked the Viewpager left and right sliding effect, the experience is very bad. And in the process of sliding the Viewpager, the user sliding is certainly not always horizontal, there will be a certain degree of downward sliding.
Viewpager handling conflicts and map handling conflicts are somewhat different because when the user clicks to Viewpager, during the swipe process. Basically it can be predicted that the user wants to swipe left and right viewpager or swipe up or down scrollview (or drop-down refresh), which cannot be like a map. ScrollView Truncation of Click events (or Swiperefreshlayout pull-down refresh) is prohibited by clicking on the Viewpager, which requires inference during the swipe process.
The solution is to set a threshold value, once the user in the x-axis is the transverse sliding distance beyond this threshold, I think the user is to swipe left and right viewpager. It is forbidden to ScrollView truncate click events at the same time set swiperefreshlayout cannot drop-down refresh. When the user lifts his finger, it feels that the user has completed the Viewpager operation and restores the ScrollView and swiperefreshlayout states.
Mviewpager.setontouchlistener (NewView.ontouchlistener () {@Override Public Boolean OnTouch(View V, motionevent event) {intAction = Event.getaction ();if(action = = Motionevent.action_down) {//Record the x-coordinate of your finger when you click to ViewpagerMLASTX = Event.getx (); }if(action = = motionevent.action_move) {//Exceeding threshold value if(Math.Abs (Event.getx ()-MLASTX) > -f) {mrefreshlayout.setenabled (false); Mscrollview.requestdisallowintercepttouchevent (true); } }if(action = = motionevent.action_up) {//user lifts the finger to restore the parent layout stateMscrollview.requestdisallowintercepttouchevent (false); Mrefreshlayout.setenabled (true); }return false; }});
When the user clicks to Viewpager, the x-coordinate of the click position is recorded, and when the user is sliding, it is assumed that the slide above the x-axis exceeds the threshold (I write 60f, which can set the best threshold value in the actual use). It is forbidden to ScrollView to truncate the Click event, at the same time set not to drop the refresh. When the user fingers leave the screen. Restores the state of ScrollView and Swiperefreshlayout.
3.3 ListView Sliding Conflict resolution
A variety of strange problems arise when nesting a ListView in ScrollView. For example, the ListView shows a problem, maybe only one or two item is that high. is not fully unfolded.
There are two ways to solve this problem online.
- Multiply the number of display data by the height of each item. Calculates the overall height of the ListView and then dynamically sets the height of the listview
- The method of copying the ListView
onMeasure(int widthMeasureSpec, int heightMeasureSpec) , let the listview unfold completely
Both of these methods can solve the problem that the ListView does not show completely. And can also slide (in fact using ScrollView's sliding effect), but there is one of the biggest regrets. The view inside the ListView is not reusable. Since both methods calculate all the height of the ListView, then set the height of the ListView control to this height. In this case, the ListView is equivalent to a linearlayout layout. Lost the advantage of reusing view. And in some scenarios may not be linearlayout useful. What's more, if you have a large number of pictures, very easy to go to Oom. This is the last thing you want to see in the development process.
Can take a look at the United States Regiment, the home of the regiment. is a scrollview, when the decline will find, and can not be unlimited downward swipe, to the bottom will be reminded to jump to a level two page to see all the group purchase information. This is a way of dealing with the layout of nested ListView lists in ScrollView.
But in the project I met, I couldn't handle it like this.
The two solutions mentioned above are very clear, and it is important to determine the height of the listview if you want the ListView to display properly.
So first. I need to set the height of the ListView in the layout file. is a clear value. After setting the height. Suppose the data in the ListView has a total item height that exceeds the height set by the ListView, it is able to reuse the view. But this is only to conquer the display of the ListView problem, the ListView and ScrollView sliding conflict, and did not resolve.
To resolve a sliding conflict, it is essential to determine when to prohibit ScrollView truncation of click events, and then to analyze the timing.
- ScrollView When you are not sliding the part, assume that you click and slide the ListView. The ListView cannot be slid. Not prohibited.
- Suppose ScrollView slides to the bottom, and the ListView has been to the top, and continues the pull-down ListView, which in fact will pull scrollview, not forbidden.
- Assume that the scrollview slides to the bottom and the user slides up. ListView Swipe to disable ScrollView truncation of Click event capability
It is very obvious that you need to know if the scrollview is sliding to the bottom when you infer that the ScrollView is forbidden to truncate the Click event. So. Rewrite the ScrollView ScrollChanged() method to infer whether ScrollView is sliding to the exact part (SDK API 23 version number ScrollView can set Setonscrollchangelistener () to listen for changes in the slide, However, the previous version number is not supported. For compatibility. You need to rewrite it).
@Overrideprotected void onscrollchanged(intLintTintOLDL,intOldt) {Super. onscrollchanged (L,t,oldl,oldt);//sliding distance plus the height of itself and the height of the sub-view control if(T + getheight () >= Getchildat (0). Getmeasuredheight ()) {//ScrollView sliding to the exact part if(Monscrolltobottomlistener! =NULL) {Monscrolltobottomlistener.onscrolltobottom (); } }Else{if(Monscrolltobottomlistener! =NULL) {Monscrolltobottomlistener.onnotscrolltobottom (); } }} Public void Setscrolltobottomlistener(Onscrolltobottomlistener Listener) { This. Monscrolltobottomlistener = Listener;} Public interface onscrolltobottomlistener { voidOnscrolltobottom ();voidOnnotscrolltobottom ();}
With the idea, and ScrollView sliding the logo can also be obtained, the following can be directly to solve the sliding conflict. Look directly at the code.
Mscrollview.setscrolltobottomlistener (NewBottomscrollview.onscrolltobottomlistener () {@Override Public void Onscrolltobottom() {Issvtobottom =true; }@Override Public void Onnotscrolltobottom() {Issvtobottom =false; }}); Mlistview.setontouchlistener (NewView.ontouchlistener () {@Override Public Boolean OnTouch(View V, motionevent event) {intAction = Event.getaction ();if(action = = Motionevent.action_down) {mlasty = Event.gety (); }if(action = = motionevent.action_move) {inttop = Mlistview.getchildat (0). GetTop ();floatNowy = Event.gety ();if(!issvtobottom) {//Agree ScrollView intercept Click event, ScrollView swipeMscrollview.requestdisallowintercepttouchevent (false); }Else if(top = =0&& nowy-mlasty > Threshold_y_list_view) {//Agree ScrollView intercept Click event, ScrollView swipeMscrollview.requestdisallowintercepttouchevent (false); }Else{//Disagree with ScrollView intercept Click event, ListView swipeMscrollview.requestdisallowintercepttouchevent (true); } }return false; }});
The sliding conflict between the ListView and the ScrollView is more difficult to solve than the other controls, but in fact it is not recommended that ScrollView nested within the ListView, once the business is complex, very easy to have various UI and business logic conflicts of error.
4. Effect of execution
Because the map adds more trouble. So no maps were introduced in the demo. Take a look at the execution effect.
Figure-2 Execution effect 5. Summarize
This article is just a way of thinking about providing a solution. In detailed scenarios, interactions are often tailored to the needs of the business in detail. However, it is important to find out when a click event is truncated and processed, and always find the solution around this key point.
Attached Demoproject address: Demoproject address link
Sliding conflict handling in Android practice ScrollView