12

GestureDetectorでLongPressが落ち着いた後、移動イベントを聞くにはどうすればよいですか?

ユーザーがLongClickすると、選択モードが開始され、正方形を画面にドラッグできます。しかし、LongPressが消費された後、onScrollが呼び出されないことに気づきました。

4

2 に答える 2

21

しばらくこれと戦おうとしましたが、今のところ解決策は次のとおりです。

  1. ジェスチャ検出器で setIsLongpressEnabled(isLongpressEnabled) を使用して長押しを無効にします

これが私のViewのOnTouchメソッドです:

public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event)== true)
        {
            //Fling or other gesture detected (not logpress because it is disabled)
        }
        else
        {
            //Manually handle the event.
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                //Remember the time and press position
                Log.e("test","Action down");
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE)
            {
                //Check if user is actually longpressing, not slow-moving 
                // if current position differs much then press positon then discard whole thing
                // If position change is minimal then after 0.5s that is a longpress. You can now process your other gestures 
                Log.e("test","Action move");
            }
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                //Get the time and position and check what that was :)
                Log.e("test","Action down");
            }

        }
        return true;
    }

画面に指を置くたびに、デバイスが ACTION_MOVE を返しました。そうでない場合は、タイマーまたはスレッドを使用して、0.5 秒後に押されたフラグの状態を確認してください。

それが役立つことを願っています!

于 2011-04-22T20:43:22.017 に答える
4

次の概念を使用して、このタスクを実行しました。

画像ビューがあり、それを長押しすると、この画像ビュー内の画像がドラッグ可能になり、別のビュー (相対レイアウトなど) 内に配置されます。画像ビューの setOnLongClickListener() メソッドで MyClickListner を設定します。

 private final class MyClickListener implements View.OnLongClickListener {

    // called when the item is long-clicked
    @Override
    public boolean onLongClick(View view) {
        // TODO Auto-generated method stub

        // create it from the object's tag
        ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());

        String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
        ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

        view.startDrag( data, //data to be dragged
                shadowBuilder, //drag shadow
                view, //local data about the drag and drop operation
                0   //no needed flags
        );
        //  view.setVisibility(View.INVISIBLE);
        return true;
    }
}

次に、相対レイアウトで MyDragListner を設定します (例: bigImageRelativeLayoutVw.setOnDragListener(new MyDragListener());)。

 class MyDragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {

        int X=(int)event.getX();
        int Y=(int)event.getY();
        int touchX = 0,touchY=0;
        // Handles each of the expected events
        switch (event.getAction()) {

            //signal for the start of a drag and drop operation.
            case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;

            //the drag point has entered the bounding box of the View
            case DragEvent.ACTION_DRAG_ENTERED:


                break;

            //the user has moved the drag shadow outside the bounding box of the View
            case DragEvent.ACTION_DRAG_EXITED:
                //    v.setBackground(normalShape); //change the shape of the view back to normal
                break;

            //drag shadow has been released,the drag point is within the bounding box of the View
            case DragEvent.ACTION_DROP:
                // if the view is the bottomlinear, we accept the drag item
                if(v == bigImageRelativeLayoutVw) {
                    View view = (View) event.getLocalState();


                    touchX=X-viewCoords[0]-20;
                    touchY=Y-viewCoords[1]-20;


                    View view1=new View(getActivity());
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30,30);

                    layoutParams.leftMargin =touchX;
                    layoutParams.topMargin = touchY;


                  view1.setBackgroundResource(R.drawable.heavy_damage);




                    view1.setLayoutParams(layoutParams);
                    RelativeLayout containView = (RelativeLayout) v;
                    containView.addView(view1);


                    view.setVisibility(View.VISIBLE);

                } else {
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);

                    break;
                }
                break;

            //the drag and drop operation has concluded.
            case DragEvent.ACTION_DRAG_ENDED:
                //     v.setBackground(normalShape);    //go back to normal shape

            default:
                break;
        }
        return true;
    }
}
于 2015-08-21T10:05:16.440 に答える