Ejemplo n.º 1
0
 public void registerBookmarkDragHandler(View view) {
   view.setOnDragListener(mBookmarkDragListener);
 }
  /**
   * This is where we initialize the fragment's UI and attach some event listeners to UI components.
   */
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContentView = inflater.inflate(R.layout.content_welcome, null);
    final ImageView imageView = (ImageView) mContentView.findViewById(R.id.image);
    mContentView.setDrawingCacheEnabled(false);

    // Handle drag events when a list item is dragged into the view
    mContentView.setOnDragListener(
        new View.OnDragListener() {
          public boolean onDrag(View view, DragEvent event) {
            switch (event.getAction()) {
              case DragEvent.ACTION_DRAG_ENTERED:
                view.setBackgroundColor(getResources().getColor(R.color.drag_active_color));
                break;

              case DragEvent.ACTION_DRAG_EXITED:
                view.setBackgroundColor(Color.TRANSPARENT);
                break;

              case DragEvent.ACTION_DRAG_STARTED:
                return processDragStarted(event);

              case DragEvent.ACTION_DROP:
                view.setBackgroundColor(Color.TRANSPARENT);
                return processDrop(event, imageView);
            }
            return false;
          }
        });

    // Show/hide the system status bar when single-clicking a photo.
    mContentView.setOnClickListener(
        new OnClickListener() {
          public void onClick(View view) {
            if (mCurrentActionMode != null) {
              // If we're in an action mode, don't toggle the action bar
              return;
            }

            if (mSystemUiVisible) {
              setSystemUiVisible(false);
            } else {
              setSystemUiVisible(true);
            }
          }
        });

    // When long-pressing a photo, activate the action mode for selection, showing the
    // contextual action bar (CAB).
    mContentView.setOnLongClickListener(
        new View.OnLongClickListener() {
          public boolean onLongClick(View view) {
            if (mCurrentActionMode != null) {
              return false;
            }

            mCurrentActionMode = getActivity().startActionMode(mContentSelectionActionModeCallback);
            view.setSelected(true);
            return true;
          }
        });

    return mContentView;
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
    View v = inflater.inflate(R.layout.drag_drop_zone, container, false);

    dropMessage = (TextView) v.findViewById(R.id.dropmessage);

    dropTarget = (View) v.findViewById(R.id.droptarget);
    dropTarget.setOnDragListener(
        new View.OnDragListener() {
          private static final String DROPTAG = "DropTarget";
          private int dropCount = 0;
          private ObjectAnimator anim;

          public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            boolean result = true;
            switch (action) {
              case DragEvent.ACTION_DRAG_STARTED:
                Log.v(DROPTAG, "drag started in dropTarget");
                break;
              case DragEvent.ACTION_DRAG_ENTERED:
                Log.v(DROPTAG, "drag entered dropTarget");
                anim = ObjectAnimator.ofFloat((Object) v, "alpha", 1f, 0.5f);
                anim.setInterpolator(new CycleInterpolator(40));
                anim.setDuration(30 * 1000); // 30 seconds
                anim.start();
                break;
              case DragEvent.ACTION_DRAG_EXITED:
                Log.v(DROPTAG, "drag exited dropTarget");
                if (anim != null) {
                  anim.end();
                  anim = null;
                }
                break;
              case DragEvent.ACTION_DRAG_LOCATION:
                Log.v(
                    DROPTAG,
                    "drag proceeding in dropTarget: " + event.getX() + ", " + event.getY());
                break;
              case DragEvent.ACTION_DROP:
                Log.v(DROPTAG, "drag drop in dropTarget");
                if (anim != null) {
                  anim.end();
                  anim = null;
                }

                ClipData data = event.getClipData();
                Log.v(DROPTAG, "Item data is " + data.getItemAt(0).getText());

                dropCount++;
                String message = dropCount + " drop";
                if (dropCount > 1) message += "s";
                dropMessage.setText(message);
                break;
              case DragEvent.ACTION_DRAG_ENDED:
                Log.v(DROPTAG, "drag ended in dropTarget");
                if (anim != null) {
                  anim.end();
                  anim = null;
                }
                break;
              default:
                Log.v(DROPTAG, "other action in dropzone: " + action);
                result = false;
            }
            return result;
          }
        });
    return v;
  }