Android 如何将ontouchevent()与onitemclick侦听器一起使用?

Android 如何将ontouchevent()与onitemclick侦听器一起使用?,android,onitemclicklistener,Android,Onitemclicklistener,我正在尝试为网格视图创建水平滚动视图。我成功地使用水平滚动视图。但是现在我也需要为相同的网格视图实现一个新的侦听器。我使用GestureDetector.SimpleOnGestureListener和onTouchEvent进行水平滚动。如果使用此选项,McClickListener将无法工作。有人帮我把这两个都搞定。提前感谢。我最近不得不处理同一个问题,我就是这样解决的 首先,我在ListFragment/ListActivity中重写默认的onListItemClicklistener。然

我正在尝试为网格视图创建水平滚动视图。我成功地使用水平滚动视图。但是现在我也需要为相同的网格视图实现一个新的侦听器。我使用GestureDetector.SimpleOnGestureListener和onTouchEvent进行水平滚动。如果使用此选项,McClickListener将无法工作。有人帮我把这两个都搞定。提前感谢。

我最近不得不处理同一个问题,我就是这样解决的

首先,我在ListFragment/ListActivity中重写默认的
onListItemClick
listener。然后在
onTouchEvent
方法中,我设置了一个条件,当为true时,调用ListView的
onListItemClick
。当操作等于
MotionEvent.action\u UP
且满足条件时,将执行此调用。您必须首先确定哪些事件组构成了视图的点击。我宣布我的动作是
向下
,紧接着是
向上
。当您准备好执行
onListItemClick
时,必须将实际项目的视图、位置和id传递给该方法

请参见下面的示例

Class....{
    private boolean clickDetected = true;

    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        //if an action other than ACTION_UP and ACTION_DOWN was performed
        //then we are no longer in a simple item click event group 
        //(i.e DOWN followed immediately by UP)
        if (action != MotionEvent.ACTION_UP
                && action != MotionEvent.ACTION_DOWN)
            clickDetected = false;

        if (action == MotionEvent.ACTION_UP){
            //check if the onItemClick requirement was met
            if (clickDetected){
                //get the item and necessary data to perform onItemClick
                //subtract the first visible item's position from the current item's position
                //to compensate for the list been scrolled since pointToPosition does not consider it
                int position = pointToPosition(x,y) - getFirstVisiblePosition();
                View view = getChildAt(position);
                if (view != null){//only continue if a valid view exists
                    long id = view.getId();
                    performItemClick(view, position, id);//pass control back to fragment/activity
                }//end if
            }//end if

            clickDetected= true;//set this to true to refresh for next event
        }//end if
        return super.onTouchEvent(ev);
        ....
    }//end onTouchEvent
}//end class
这种设置允许很大的灵活性,例如,如果您想设置长时间单击,您可以执行与上面相同的操作,并简单地检查“向下”和“向上”操作之间的时间差


另一种方法是获取启动向下操作的项目的位置,并将其与向上操作的项目进行对比,甚至为向下和向上操作引入时间差标准(比如小于1秒)。

好的,这是我登录的原因。无论如何,我想说的是,这在一些低成本设备上是行不通的。不久前,我在测试我的新应用程序时注意到,在三星、华硕、gigabite(以及其他常见设备)上,查看注册的ACTION\u DOWN时,紧接着是ACTION\u UP ACTION,在低成本设备上,它是ACTION\u DOWN->ACTION\u MOVE->ACTION\u UP。