Last active
October 18, 2018 14:25
-
-
Save h6ah4i/49e56a65d1921e0f9996 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.h6ah4i.android.example.dragdrop; | |
import android.app.Activity; | |
import android.graphics.Canvas; | |
import android.graphics.Point; | |
import android.os.Bundle; | |
import android.view.DragEvent; | |
import android.view.LayoutInflater; | |
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.view.WindowManager; | |
import android.widget.AdapterView; | |
import android.widget.BaseAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setContentView(R.layout.activity_main); | |
ListView lv = (ListView) findViewById(android.R.id.list); | |
List<String> items = new ArrayList<>(); | |
for (int i = 0; i < 20; i++) { | |
items.add(Integer.toString(i)); | |
} | |
lv.setAdapter(new MyAdapter(items)); | |
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
@Override | |
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | |
// | |
// start dragging | |
// | |
MyAdapter.ViewHolder vh = (MyAdapter.ViewHolder) view.getTag(); | |
final int touchedX = (int) (vh.lastTouchedX + 0.5f); | |
final int touchedY = (int) (vh.lastTouchedY + 0.5f); | |
view.startDrag(null, new DragShadowBuilder(view) { | |
@Override | |
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { | |
super.onProvideShadowMetrics(shadowSize, shadowTouchPoint); | |
shadowTouchPoint.x = touchedX; | |
shadowTouchPoint.y = touchedY; | |
} | |
@Override | |
public void onDrawShadow(Canvas canvas) { | |
super.onDrawShadow(canvas); | |
} | |
}, view, 0); | |
view.setVisibility(View.INVISIBLE); | |
return true; | |
} | |
}); | |
lv.setOnDragListener(new OnDragListener() { | |
@Override | |
public boolean onDrag(View v, DragEvent event) { | |
if (event.getAction() == DragEvent.ACTION_DROP) { | |
// | |
// finish dragging | |
// | |
View view = (View) event.getLocalState(); | |
view.setVisibility(View.VISIBLE); | |
} | |
return true; | |
} | |
}); | |
} | |
static class MyAdapter extends BaseAdapter implements OnTouchListener { | |
private List<String> mItems; | |
private static class ViewHolder { | |
public TextView text; | |
public float lastTouchedX; | |
public float lastTouchedY; | |
public ViewHolder(View v) { | |
text = (TextView) v; | |
} | |
} | |
MyAdapter(List<String> items) { | |
mItems = items; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View view; | |
ViewHolder vh; | |
if (convertView == null) { | |
view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); | |
view.setOnTouchListener(this); | |
vh = new ViewHolder(view); | |
view.setTag(vh); | |
} else { | |
view = convertView; | |
vh = (ViewHolder) view.getTag(); | |
} | |
vh.text.setText(mItems.get(position)); | |
return view; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return 0; | |
} | |
@Override | |
public int getCount() { | |
return mItems.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return mItems.get(position); | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
ViewHolder vh = (ViewHolder) v.getTag(); | |
vh.lastTouchedX = event.getX(); | |
vh.lastTouchedY = event.getY(); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unfortunately drop is not working