Skip to content

Commit

Permalink
Use raw coordinates for touch events. This fixes issues when there's …
Browse files Browse the repository at this point in the history
…a view under the child view that "steals" the event (like a button) and makes the coordinates relative to itself, not the list view.
  • Loading branch information
pkulak committed Dec 20, 2011
1 parent de8946f commit c44957e
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,9 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2,

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Rect viewRect = new Rect();
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int left = child.getLeft();
int right = child.getRight();
int top = child.getTop();
int bottom = child.getBottom();
viewRect.set(left, top, right, bottom);
if(viewRect.contains((int)e.getX(), (int)e.getY())){
if (isEventWithinView(e, child)) {
if(mOnItemClicked != null){
mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
Expand All @@ -362,16 +356,10 @@ public boolean onSingleTapConfirmed(MotionEvent e) {

@Override
public void onLongPress(MotionEvent e) {
Rect viewRect = new Rect();
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int left = child.getLeft();
int right = child.getRight();
int top = child.getTop();
int bottom = child.getBottom();
viewRect.set(left, top, right, bottom);
if (viewRect.contains((int) e.getX(), (int) e.getY())) {
if (isEventWithinView(e, child)) {
if (mOnItemLongClicked != null) {
mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));
}
Expand All @@ -381,6 +369,17 @@ public void onLongPress(MotionEvent e) {
}
}

private boolean isEventWithinView(MotionEvent e, View child) {
Rect viewRect = new Rect();
int[] childPosition = new int[2];
child.getLocationOnScreen(childPosition);
int left = childPosition[0];
int right = left + child.getWidth();
int top = childPosition[1];
int bottom = top + child.getHeight();
viewRect.set(left, top, right, bottom);
return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
};


Expand Down

0 comments on commit c44957e

Please sign in to comment.