|
2 | 2 |
|
3 | 3 | import android.app.Activity;
|
4 | 4 | import android.content.Context;
|
| 5 | +import android.graphics.Rect; |
5 | 6 | import android.util.AttributeSet;
|
6 | 7 | import android.view.MotionEvent;
|
7 | 8 | import android.view.View;
|
8 | 9 | import android.view.ViewGroup;
|
| 10 | +import android.view.ViewParent; |
9 | 11 | import android.view.ViewTreeObserver;
|
| 12 | +import android.view.Window; |
10 | 13 | import android.webkit.WebView;
|
11 | 14 | import android.widget.AbsListView;
|
12 | 15 | import android.widget.ScrollView;
|
@@ -215,7 +218,9 @@ private boolean isSwipeIndirect(MotionEvent ev) {
|
215 | 218 | public boolean onInterceptTouchEvent(MotionEvent ev) {
|
216 | 219 | boolean handled = false;
|
217 | 220 | ensureTarget();
|
218 |
| - findTouchedFamily(ev); |
| 221 | + if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 222 | + findTouchedFamily(ev); |
| 223 | + } |
219 | 224 | if (isEnabled() && isSwipeEnabled && !isSwipeIndirect(ev)) {
|
220 | 225 | handled = viewDragHelper.shouldInterceptTouchEvent(ev);
|
221 | 226 | } else {
|
@@ -265,11 +270,58 @@ private void findScrollableChildren(ViewGroup viewGroup){
|
265 | 270 |
|
266 | 271 | private void findTouchedFamily(MotionEvent ev){
|
267 | 272 | touchedFamily = new ArrayList<>();
|
| 273 | + |
| 274 | + Rect rectangle = new Rect(); |
| 275 | + Window window = ((Activity)getContext()).getWindow(); |
| 276 | + window.getDecorView().getWindowVisibleDisplayFrame(rectangle); |
| 277 | + int statusBarHeight = rectangle.top; |
| 278 | + |
268 | 279 | int x = Math.round(ev.getX());
|
269 |
| - int y = Math.round(ev.getY()); |
| 280 | + int y = Math.round(ev.getY() + statusBarHeight); |
270 | 281 | for (View scrollable: scrollableChildren) {
|
271 |
| - if (x > scrollable.getLeft() && x < scrollable.getRight() && y > scrollable.getTop() && y < scrollable.getBottom()) { |
272 |
| - touchedFamily.add(scrollable); |
| 282 | + |
| 283 | + Rect scrollableRect = new Rect(); |
| 284 | + scrollable.getGlobalVisibleRect(scrollableRect); |
| 285 | + |
| 286 | + if (x > scrollableRect.left && x < scrollableRect.right && y > scrollableRect.top && y < scrollableRect.bottom) { |
| 287 | + if(!touchedFamily.contains(scrollable)) { |
| 288 | + touchedFamily.add(scrollable); |
| 289 | + } |
| 290 | + addFamily(scrollable); |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + private void addFamily(View view){ |
| 296 | + addParents(view); |
| 297 | + if(view instanceof CoordinatorLayout) { |
| 298 | + addChildren((ViewGroup) view); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + private void addParents(View view){ |
| 303 | + ViewParent parent = view.getParent(); |
| 304 | + if(parent instanceof View){ |
| 305 | + if(!touchedFamily.contains((View)parent) && scrollableChildren.contains((View)parent)){ |
| 306 | + touchedFamily.add((View) parent); |
| 307 | + } |
| 308 | + addParents((View) parent); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + private void addChildren(ViewGroup viewGroup){ |
| 313 | + if (viewGroup.getChildCount() > 0) { |
| 314 | + int count = viewGroup.getChildCount(); |
| 315 | + for (int i = 0; i < count; i++) { |
| 316 | + View child = viewGroup.getChildAt(i); |
| 317 | + if (child instanceof AbsListView || child instanceof RecyclerView || child instanceof ScrollView || child instanceof NestedScrollView) { |
| 318 | + if(!touchedFamily.contains((View)child) && scrollableChildren.contains((View)child)) { |
| 319 | + touchedFamily.add(child); |
| 320 | + } |
| 321 | + } |
| 322 | + if (child instanceof ViewGroup) { |
| 323 | + addChildren((ViewGroup) child); |
| 324 | + } |
273 | 325 | }
|
274 | 326 | }
|
275 | 327 | }
|
|
0 commit comments