Android-在应用中使用拖拽教程

Android-在应用中使用拖拽教程

Lars Vogel

Version 1.3
Copyright © 2012-2014 vogella GmbH
21.11.2014
在Android中使用拖拽

这个教程描述在Android中如何使用拖拽


目录

  1. Android拖拽
    1.1在Android中使用拖拽
    1.2允许view拖拽
    1.3定义拖动目标
  2. 练习:拖拽
    2.1这个练习的目标
    2.2创建项目
    2.3创建 xml Drawable
    2.4Activity和layout

1.Android拖拽

1.1在Android中使用拖拽
Android4.0支持在view或viewGroup上的拖拽view
1.2允许view拖拉
你注册一个onTouchListener或LongClickListener在被拖拽的view上使其能够拖拽
View的startDrag方法开始一个拖拽操作。在这个方法你也可以通过ClipData的实例指定按下拖拽目标数据
你也可以通过DragShadowBuilder实例的startDrag方法。这个对象指定图像用于拖拽操作。例如你可以直接地通过View,在拖拽操作中展现一个view的图像
下面的例子演示在TouchListener下的拖拉操作设置

    // Assign the touch listener to your view which you want to move
            findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());

    // This defines your touch listener
    private final class MyTouchListener implements OnTouchListener {
      public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
  ClipData data = ClipData.newPlainText("", "");
  DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
  view.startDrag(data, shadowBuilder, view, 0);
  view.setVisibility(View.INVISIBLE);
  return true;
} else {
return false;
}
     }
    } 

1.3 定义拖拉目标

能够拖拽的view得到一个onDragListener分配的实例。在这个拖拽监听,你接收预定义的拖拽相关事件回调

  • DragEvent.ACTION_DRAG_STARTED
  • DragEvent.ACTION_DRAG_ENTERED
  • DragEvent.ACTION_DRAG_EXITED
  • DragEvent.ACTION_DROP
  • DragEvent.ACTION_DRAG_ENDED
    一个有onDragListener的view是可以拖拽的区域,通过setonDragListener得到一个分配onDragListener

     findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
    
    class MyDragListener implements OnDragListener {
      Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
      Drawable normalShape = getResources().getDrawable(R.drawable.shape);
    
      @Override
      public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
    // do nothing
      break;
      case DragEvent.ACTION_DRAG_ENTERED:
        v.setBackgroundDrawable(enterShape);
        break;
      case DragEvent.ACTION_DRAG_EXITED:        
        v.setBackgroundDrawable(normalShape);
        break;
      case DragEvent.ACTION_DROP:
        // Dropped, reassign View to ViewGroup
        View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        LinearLayout container = (LinearLayout) v;
        container.addView(view);
        view.setVisibility(View.VISIBLE);
        break;
      case DragEvent.ACTION_DRAG_ENDED:
        v.setBackgroundDrawable(normalShape);
        default:
        break;
      }
      return true;
    }
    }
    

练习:拖拽

2.1练习目标
在这个练习中,你创建多个允许他们之间拖动views的Viewgroup
2.2创建项目
创建一个新的名为com.vogella.android.draganddrop的Android 项目和一个叫DragActivity的Activity
创建 XMLDrawables
在这个练习你能够使用 XML Drawable
在这个部分你在res/drawable文件夹创建多个XML Drawable
创建下面shape.xml文件在这个文件夹

    








  

也创建下面的shape——droptarget.xml文件


 







  

2.4Activity和layout
用下面的代码的修改Activity的layout

        
    



    




    




    




    



用下面的代码修改你的activity

        package com.vogella.android.draganddrop;

import android.app.Activity;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class DragActivity extends Activity {

/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

  }

  private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

  class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

@Override
public boolean onDrag(View v, DragEvent event) {
  int action = event.getAction();
  switch (event.getAction()) {
  case DragEvent.ACTION_DRAG_STARTED:
    // do nothing
    break;
  case DragEvent.ACTION_DRAG_ENTERED:
    v.setBackgroundDrawable(enterShape);
    break;
  case DragEvent.ACTION_DRAG_EXITED:
    v.setBackgroundDrawable(normalShape);
    break;
  case DragEvent.ACTION_DROP:
    // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    owner.removeView(view);
    LinearLayout container = (LinearLayout) v;
    container.addView(view);
    view.setVisibility(View.VISIBLE);
    break;
  case DragEvent.ACTION_DRAG_ENDED:
    v.setBackgroundDrawable(normalShape);
  default:
    break;
  }
  return true;
}
  }
} 

如果你开启这个activity,你应该能够拖动ImageView到其他容
Android-在应用中使用拖拽教程_第1张图片
原文地址

你可能感兴趣的:(Android)