Skip to content

Commit a7a404f

Browse files
committed
more accurate and more efficient way of calculating touched area
1 parent 0cd439d commit a7a404f

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
SwipeDismissLayout
44
===================
55

6+
v1.3.5 by mohapps:
7+
8+
more accurate and more efficient way of calculating touched area
9+
610
v1.3.0 by mohapps:
711

812
whether to swipe for dismiss or not will be determined based on touch area rather than a single child view's scrollability
@@ -51,7 +55,7 @@ Add it in your root build.gradle at the end of repositories:
5155
Add the dependency
5256

5357
dependencies {
54-
implementation 'com.github.mohappsdev:SwipeDismissLayout:v1.3.0'
58+
implementation 'com.github.mohappsdev:SwipeDismissLayout:v1.3.5'
5559
}
5660

5761
Set translucent theme for that activity in AndroidManifest:
@@ -96,7 +100,7 @@ If translucent theme causes your dismissable activity layout background to be tr
96100
----------
97101
Version
98102
-------------
99-
1.3.0
103+
1.3.5
100104

101105
----------
102106

swipedismisslayout/src/main/java/com/viewgroup/SwipeDismissLayout.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.graphics.Rect;
56
import android.util.AttributeSet;
67
import android.view.MotionEvent;
78
import android.view.View;
89
import android.view.ViewGroup;
10+
import android.view.ViewParent;
911
import android.view.ViewTreeObserver;
12+
import android.view.Window;
1013
import android.webkit.WebView;
1114
import android.widget.AbsListView;
1215
import android.widget.ScrollView;
@@ -215,7 +218,9 @@ private boolean isSwipeIndirect(MotionEvent ev) {
215218
public boolean onInterceptTouchEvent(MotionEvent ev) {
216219
boolean handled = false;
217220
ensureTarget();
218-
findTouchedFamily(ev);
221+
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
222+
findTouchedFamily(ev);
223+
}
219224
if (isEnabled() && isSwipeEnabled && !isSwipeIndirect(ev)) {
220225
handled = viewDragHelper.shouldInterceptTouchEvent(ev);
221226
} else {
@@ -265,11 +270,58 @@ private void findScrollableChildren(ViewGroup viewGroup){
265270

266271
private void findTouchedFamily(MotionEvent ev){
267272
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+
268279
int x = Math.round(ev.getX());
269-
int y = Math.round(ev.getY());
280+
int y = Math.round(ev.getY() + statusBarHeight);
270281
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+
}
273325
}
274326
}
275327
}

0 commit comments

Comments
 (0)