Skip to content

Commit c44957e

Browse files
committed
Use raw coordinates for touch events. This fixes issues when there's a view under the child view that "steals" the event (like a button) and makes the coordinates relative to itself, not the list view.
1 parent de8946f commit c44957e

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,9 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2,
338338

339339
@Override
340340
public boolean onSingleTapConfirmed(MotionEvent e) {
341-
Rect viewRect = new Rect();
342341
for(int i=0;i<getChildCount();i++){
343342
View child = getChildAt(i);
344-
int left = child.getLeft();
345-
int right = child.getRight();
346-
int top = child.getTop();
347-
int bottom = child.getBottom();
348-
viewRect.set(left, top, right, bottom);
349-
if(viewRect.contains((int)e.getX(), (int)e.getY())){
343+
if (isEventWithinView(e, child)) {
350344
if(mOnItemClicked != null){
351345
mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
352346
}
@@ -362,16 +356,10 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
362356

363357
@Override
364358
public void onLongPress(MotionEvent e) {
365-
Rect viewRect = new Rect();
366359
int childCount = getChildCount();
367360
for (int i = 0; i < childCount; i++) {
368361
View child = getChildAt(i);
369-
int left = child.getLeft();
370-
int right = child.getRight();
371-
int top = child.getTop();
372-
int bottom = child.getBottom();
373-
viewRect.set(left, top, right, bottom);
374-
if (viewRect.contains((int) e.getX(), (int) e.getY())) {
362+
if (isEventWithinView(e, child)) {
375363
if (mOnItemLongClicked != null) {
376364
mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));
377365
}
@@ -381,6 +369,17 @@ public void onLongPress(MotionEvent e) {
381369
}
382370
}
383371

372+
private boolean isEventWithinView(MotionEvent e, View child) {
373+
Rect viewRect = new Rect();
374+
int[] childPosition = new int[2];
375+
child.getLocationOnScreen(childPosition);
376+
int left = childPosition[0];
377+
int right = left + child.getWidth();
378+
int top = childPosition[1];
379+
int bottom = top + child.getHeight();
380+
viewRect.set(left, top, right, bottom);
381+
return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
382+
}
384383
};
385384

386385

0 commit comments

Comments
 (0)