@@ -338,15 +338,9 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2,
338
338
339
339
@ Override
340
340
public boolean onSingleTapConfirmed (MotionEvent e ) {
341
- Rect viewRect = new Rect ();
342
341
for (int i =0 ;i <getChildCount ();i ++){
343
342
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 )) {
350
344
if (mOnItemClicked != null ){
351
345
mOnItemClicked .onItemClick (HorizontalListView .this , child , mLeftViewIndex + 1 + i , mAdapter .getItemId ( mLeftViewIndex + 1 + i ));
352
346
}
@@ -362,16 +356,10 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
362
356
363
357
@ Override
364
358
public void onLongPress (MotionEvent e ) {
365
- Rect viewRect = new Rect ();
366
359
int childCount = getChildCount ();
367
360
for (int i = 0 ; i < childCount ; i ++) {
368
361
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 )) {
375
363
if (mOnItemLongClicked != null ) {
376
364
mOnItemLongClicked .onItemLongClick (HorizontalListView .this , child , mLeftViewIndex + 1 + i , mAdapter .getItemId (mLeftViewIndex + 1 + i ));
377
365
}
@@ -381,6 +369,17 @@ public void onLongPress(MotionEvent e) {
381
369
}
382
370
}
383
371
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
+ }
384
383
};
385
384
386
385
0 commit comments