实施滑动和长按列表项?

时间:2012-12-19 06:02:13

标签: android touch swipe listitem

我需要实现Longclick和Left&右键在列表视图上滑动并获取执行操作的listitem。 This方法似乎很有希望。 问题: 1.ACTION_MOVE在开始时只触发一次,因此差异非常小 2.如果我在交换机中使用默认值,我会获得最后一个位置,但onClick或onLongClick永远不会被触发。这是我试过的..是否可以触发假的ACTION以使itemClick / itemlongclick执行。

public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getAction()) 
        {
        case MotionEvent.ACTION_DOWN: 
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.NONE;
            Log.i("MyTags","Down Event");
            Log.i("MyTags",String.valueOf(downX)+","+String.valueOf(downY));
            return false; // allow other events like Click to be processed
        case MotionEvent.ACTION_MOVE: 
            upX = event.getX();
            upY = event.getY();
            Log.i("MyTags","Move Event");
            Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
            moveEnabled=true;
            return false;
        case MotionEvent.ACTION_UP:
            upX = event.getX();
            upY = event.getY();
            Log.i("MyTags","UP Event");
            Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
            return false;
        default:
            upX = event.getX();
            upY = event.getY();
            Log.i("MyTags","Default Event");
            Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
            if(moveEnabled)
            {
                diffX=downX-upX;
                diffY=downY-upY;
                abs_X=Math.abs(diffX);
                abs_Y=Math.abs(diffY);

                moveEnabled=false;
                if((abs_X>abs_Y)&(abs_X>MINIMUM_X))
                {
                    if(diffX>0)
                    {
                        mSwipeDetected=Action.LEFT;
                        Log.i("MyTags","Left Swipe");
                        event.setAction(MotionEvent.ACTION_UP);
                        return false;
                    }
                    else if(diffX<0)
                    {
                        mSwipeDetected=Action.RIGHT;
                        Log.i("MyTags","Right Swipe");
                        event.setAction(MotionEvent.ACTION_UP);
                        return false;
                    }
                }
            }
            return false;
        }

    }

1 个答案:

答案 0 :(得分:1)

我有点使用黑客来解决我的问题。发生的事情是因为事件被消耗了,我从来没有在onitemclick事件中得到结果。我所做的是在检测到滑动动作后触发我自己的Down和Up事件,在这些假事件中,我返回false,以便将其传递给itemclick侦听器。我们需要在这里小心一点,因为事件似乎从结果中读取了2次,为此我打开和关闭监听器,通过包含输出。

ACTION_MOVE:
 //when swipe is detected fire a fake down event
 event.setAction(MotionEvent.ACTION_DOWN);
 v.dispatchTouchEvent(event);
ACTION_DOWN:
  if(fake down event)
     //set some flags & dispatch fake event
     event.setAction(MotionEvent.ACTION_UP);
     v.dispatchTouchEvent(event);
     return false
   else do regular handling
ACTION_UP:
    if fake down event
       dispatch fake up event
    else ..

这里'v'是必须触发事件的视图,在我的例子中是listview。 由于我们没有正确关闭它必须通过swipeDetector类的构造函数传递。如下..

ListView v;
SwipeDetector(ListView lv)
{
    this.v=lv;
}