0

我有一个实现 OnDragListener 的类。我需要检查在视图中放置了哪个图像。请指导我如何做到这一点。

private class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) { 
        case DragEvent.ACTION_DROP: 
            View view = (View) event.getLocalState(); 
            view.setVisibility(View.INVISIBLE); 
            ImageView dropTarget = (ImageView) v; 
            ImageView dropped = (ImageView) view; 
            dropTarget.setImageDrawable(dropped.getDrawable());  
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }
} 
4

1 回答 1

0

你必须遵循一些步骤:

1) 制作 MyDragEventListener的对象。

MyDragEventListener myDragEventListener = new MyDragEventListener();

2)使用这个类来定位布局。

view.setOnDragListener(myDragEventListener);

3)

 class MyDragEventListener implements View.OnDragListener {
       @Override
       public boolean onDrag(View v, DragEvent event) {              

              final int action = event.getAction();           
              switch(action) {

              case DragEvent.ACTION_DRAG_STARTED:
                        if (event.getClipDescription()
                          .hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
                        {
                         return true; //Accept
                        }
                        else{
                         return false; //reject
                        }

              case DragEvent.ACTION_DRAG_ENTERED:
               return true;

              case DragEvent.ACTION_DRAG_LOCATION:
              return true;

              case DragEvent.ACTION_DRAG_EXITED:
              return true;

             case DragEvent.ACTION_DROP:
                  ClipData.Item item = event.getClipData().getItemAt(0);
                    if(v == view){
                            //put your logic here
                        } 
                       return true;

              case DragEvent.ACTION_DRAG_ENDED:
                  if (event.getResult())
                  {  
                  //forther any updation not require.
                  }                       
                  return true;

              default: //unknown case
                  return false;

              }

             }


        }

进口部分:

case DragEvent.ACTION_DROP:
              ClipData.Item item = event.getClipData().getItemAt(0);
                if(v == view){
                        //put your logic here
                    } 
                   return true;

如果您查询然后发表评论。

于 2013-02-14T04:36:22.510 回答