Back to project page android-drag-drop.
The source code is released under:
MIT License
If you think the Android project android-drag-drop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.mydraggable; //from ww w.j av a 2s. co m import android.R.integer; import android.app.Activity; import android.content.ClipData; import android.os.Bundle; import android.util.Log; import android.view.DragEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.DragShadowBuilder; import android.view.View.OnDragListener; import android.view.View.OnLongClickListener; import android.widget.TextView; public class MainActivity extends Activity { TextView[] mTextViews = new TextView[4]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // TextView???? int[] ids = new int[]{ R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 }; for(int i = 0; i < 4; i++){ mTextViews[i] = (TextView)findViewById(ids[i]); } // ?????? for(int i = 0; i < 4; i++){ // ???????????????? mTextViews[i].setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // ????? MyTextView tv = (MyTextView)v; ClipData data = ClipData.newPlainText("test", "drag:" + tv.getText().toString()); // ?????? v.startDrag(data, new MyDragShadowBuilder(v), v, 0); // ????????????????????????????????????????? tv.setBorderColor(0xFF0000FF); return true; } }); // ???????? mTextViews[i].setOnDragListener(new OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { MyTextView from = (MyTextView)event.getLocalState(); MyTextView to = (MyTextView)v; String pos = String.format("%s -> %s [%.2f, %.2f] ", from.getText().toString(), to.getText().toString(), event.getX(), event.getY()); switch(event.getAction()){ case DragEvent.ACTION_DRAG_STARTED: // ??????????????????????? if(from == to){ return false; } Log.d("test", pos + "drag started."); // ????????????????????????????????????????????? to.setBorderColor(0xFFFFFF00); return true; case DragEvent.ACTION_DRAG_ENDED: Log.d("test", pos + "drag ended."); // ?????????????? to.setBorderColor(0xFF000000); return true; case DragEvent.ACTION_DRAG_LOCATION: // Log.d("test", pos + "drag location."); // ?????????????????????????????? return true; case DragEvent.ACTION_DROP: // ?? Log.d("test", pos + "drop."); // ???????? ClipData data = event.getClipData(); Log.d("test", "---- clip data ----"); for(int i = 0; i < data.getItemCount(); i++){ ClipData.Item item = data.getItemAt(i); Log.d("test", " [" + i + "]"); Log.d("test", " " + item.toString()); Log.d("test", " " + item.getText().toString()); } // from?to??????????????? { ViewGroup parent = (ViewGroup)from.getParent(); parent.removeView(from); parent.addView(from, parent.indexOfChild(to)); } return true; case DragEvent.ACTION_DRAG_ENTERED: Log.d("test", pos + "drag entered."); // ?????????????????????????????????????????????? to.setBorderColor(0xFFFF0000); return true; case DragEvent.ACTION_DRAG_EXITED: Log.d("test", pos + "drag exited."); // ?????????????? to.setBorderColor(0xFFFFFF00); return true; } // TODO Auto-generated method stub return false; } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }