Android拖放动态创建的视图

时间:2015-01-22 15:25:11

标签: java android android-layout android-activity android-fragments

真的可以提供一些帮助。

我随机创建了一个随机数量的按钮,每个按钮都是setOnTouchListener,看起来工作正常。按钮被添加到Linearlayout(顶部),还有另一个空的Linearlayout(底部)。

我希望能够做到以下几点:

  1. 拖动&在Linearlayout(顶部)和Linearlayout(底部)

     public class GetString extends Activity{
    
     myDragEventListener mDragListen = new myDragEventListener();
    
      public void getString(Context context, String[] temp, int no,  LinearLayout words1, LinearLayout words2) {
    
    
       Button bt;
    
       int i = 0;
       for (i = 0; i < no; i++) {
    
         bt = new Button(context);
         bt.setId(i);
         bt.setText(temp[i]);
         bt.setOnTouchListener(new MyTouchListener());
         words1.addView(bt);
         bt.setOnDragListener(mDragListen);
       }
      }
    
    
    protected class myDragEventListener implements View.OnDragListener {
    
    
     public boolean onDrag(View v, DragEvent event) {
    
        // Defines a variable to store the action type for the incoming event
        final int action = event.getAction();
    
        // Handles each of the expected events
        switch(action) {
    
            case DragEvent.ACTION_DRAG_STARTED:
    
                return false;
    
            case DragEvent.ACTION_DRAG_ENTERED:
    
                v.invalidate();
    
                return true;
    
            case DragEvent.ACTION_DRAG_LOCATION:
    
                return true;
    
            case DragEvent.ACTION_DRAG_EXITED:
    
                v.invalidate();
    
                return true;
    
            case DragEvent.ACTION_DROP:
    
                return true;
    
            case DragEvent.ACTION_DRAG_ENDED:
    
    
    
                return true;
    
            // An unknown action type was received.
            default:
                Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
                break;
        }
    
        return false;
      }
     }
    
    
    public class MyTouchListener implements View.OnTouchListener {
    
      @Override
      public boolean onTouch(View v, MotionEvent arg1) {
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadow = new View.DragShadowBuilder(v);
        v.startDrag(data, shadow, null, 0);
        return false;
      }
    }
    
    
    }
    

0 个答案:

没有答案