Our general idea, in fact, is this, but also my assumption, we can first to implement a simple ListView data, but his adapter, we can be encapsulated by the system, and then passed into an entity class, and finally customize a ListView to operate, So we're going to do a good job, like?
List_item.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_ Parent ">
<imageview
android:id=" @+id/iv_logo "
android:layout_width=" 50DP "
android:layout _height= "50DP"
android:layout_alignparentleft= "true"
android:layout_centerinparent= "true
" android:layout_marginleft= "10DP"/>
<textview
android:id= "@+id/textview"
android:layout_ Width= "Wrap_content"
android:layout_height= "wrap_content"
android:layout_centerinparent= "true"
android:layout_marginleft= "10DP"
android:layout_torightof= "@id/iv_logo"/>
</RelativeLayout>
There's only one head and one sentence, and then we've finished with the entity class.
Dragbean
Package com.liuguilin.draglistviewsample.entity;
* * Project name: Draglistviewsample
* Package Name: com.liuguilin.draglistviewsample.entity
* FileName: Dragbean
* Creator: lgl< c6/>* creation time: 2016/8/29 22:49
* Description: Entity class
* * * Public
class Dragbean {
private int ivid;
private String text;
Public Dragbean () {
} public
dragbean (int ivid, String text) {
this.ivid = Ivid;
This.text = text;
public int Getivid () {return
ivid;
}
Public String GetText () {return
text;
}
}
OK, actually very simple, ID is the picture, then is the text, so we can come to implement a adapter, here I use is arrayadapter so that we can insert and delete is easy
Dragadapter
Package com.liuguilin.draglistviewsample.adapter; * * Project Name: Draglistviewsample * Package Name: Com.liuguilin.draglistviewsample.adapter * FileName: Dragadapter * Creator: LGL * creation time: 2016/8/
29 22:41 * Description: Drag the list of data source/import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.widget.ImageView;
Import Android.widget.TextView;
Import COM.LIUGUILIN.DRAGLISTVIEWSAMPLE.R;
Import Com.liuguilin.draglistviewsample.entity.DragBean;
Import java.util.List; public class Dragadapter extends arrayadapter<dragbean> {/** * Construction method * * @param context * @param mlist/Public Dra Gadapter (context, list<dragbean> mlist) {Super (context, 0, mlist);}/** * Implementation view * @param position * @pa Ram Convertview * @param parent * @return * * @Override public view GetView (int position, View Convertview, ViewGroup paren
T) {view view;
Viewholder Viewholder; if (Convertview = = null) {view = View.inflate (GetContext (), R.layout.list_item, nULL);
Viewholder = new Viewholder ();
Viewholder.imageview = (imageview) view. Findviewbyid (R.id.iv_logo);
Viewholder.textview = (TextView) View.findviewbyid (R.id.textview);
View.settag (Viewholder); else {view = Convertview Viewholder = (viewholder) View.gettag ();} viewHolder.imageView.setImageResource (pos
ition). Getivid ());
ViewHolder.textView.setText (GetItem (position). GetText ());
return view;
}/** * Cache/Static class Viewholder {ImageView ImageView;
TextView TextView; }
}
Well, actually here, he's one of the most common ListView, and we'll fill him in with data.
Mainactivity
Package com.liuguilin.draglistviewsample;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Com.liuguilin.draglistviewsample.adapter.DragAdapter;
Import Com.liuguilin.draglistviewsample.entity.DragBean;
Import Com.liuguilin.draglistviewsample.view.DragListView;
Import java.util.ArrayList;
Import java.util.List; public class Mainactivity extends Appcompatactivity {//list private draglistview Mlistview;//Data Private LIST<DRAGBEAN&G T
Mlist = new arraylist<> ();
Data source private Dragadapter adapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview (); /** * Initialize view/private void Initview () {Mlistview = (Draglistview) Findviewbyid (R.id.mlistview);//Add data for (int i = 0; I < 30; i++) {Dragbean bean = new Dragbean (r.mipmap.ic_launcher, "Liu someone Programmer" + i); Mlist.add (bean);}//init data Source adapter = new Dragada
Pter (this,mlist);
Mlistview.setadapter (adapter); }
}
Now we can look at the actual effect.
Now we can rewrite our listview.
We'll intercept his incident first.
/** * Get the location of the entry where the contact is located * Get the picture of the selected item * Event interception mechanism * * @param EV * @return/@Override public bool Ean onintercepttouchevent (motionevent ev) {//Identify action switch (ev.getaction ()) {case Motionevent.action_down://get the coordinate int x of the contact point
= (int) ev.getx ();
int y = (int) ev.gety ();
This allows you to calculate which entry I pressed on mstartposition = Mendposition = Pointtoposition (x, y);
Determine if the contact point is in the area of the logo viewgroup Itemview = (viewgroup) getchildat (Mstartposition-getfirstvisibleposition ());
Record the relative y-coordinate of the finger in the entry Dragpoint = Y-itemview.gettop ();
ListView the y-coordinate in the screen dragoffset = (int) (Ev.getrawy ()-y);
Drag icon View Dragger = Itemview.findviewbyid (R.id.iv_logo); Determine if the contact point is in the logo area if (dragger!= null && x < Dragger.getright () + 10) {//define ListView Scroll Entry//Up Upscroll = Getheigh
T ()/3;
Lower downscroll = getheight () * 2/3;
Gets the selected picture/screenshot itemview.setdrawingcacheenabled (true);
Get screenshots Bitmap Bitmap = Itemview.getdrawingcache ();
Picture scrolling StartDrag (BitMap, y);
} break;
//will also pass the event to the child view return false; }
Get to his position after we have a direct screenshot, and show our window, there are more things to do here we also have to decide whether to click on the Avatar to show window
/** * picture on the y-axis, that is, up and down scrollable * * @param bitMap * @param y/private void StartDrag (BitMap bitma p, int y) {//form is modeled after WM = (WindowManager) getcontext (). Getsystemservice (Context.window_service);//Set form parameters Lparams = new Wind
Owmanager.layoutparams ();
Set width High lparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
Lparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Attribute Lparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Set semitransparent Lparams.alpha = 0.8f;
Set Center lparams.gravity = gravity.top;
Set XY lparams.x = 0;
Lparams.y = Y-dragpoint + dragoffset;
attribute Lparams.format = pixelformat.translucent;
Set animation lparams.windowanimations = 0;
Picture Dragimageview = new ImageView (GetContext ());
Set screenshot dragimageview.setimagebitmap (BITMAP);
Add Display Form Wm.addview (Dragimageview, lparams); }
OK, we initialize window, the light display is not yet, we still have to be able to slide, how to monitor? Ontouchevent's Action_move events can be done.
/**
* Touch Event
*
@param ev
* @return
/@Override public
boolean ontouchevent (motionevent EV) {
//Wrong position
if (dragimageview!= null && mendposition!= invalid_position) {
//sliding control up and down in sliding events
switch (ev.getaction ()) {case
Motionevent.action_move:
//get directly to Y coordinate to move
int movey = (int) ev.gety ();
Dodrag (Movey);
break;
Stop dragging imaging case
MOTIONEVENT.ACTION_UP:
int upy = (int) ev.gety ();
Stopdrag ();
Ondrag (upy);
break;
}
}
Intercept to event return
true;
}
We keep updating his position while we're moving.
/**
* Control form Move
*
* @param movey * *
private void Dodrag (int movey) {
if (Dragimageview!= null) {
lparams.y = Movey-dragpoint + dragoffset;
Wm.updateviewlayout (Dragimageview, lparams);
}
Judge moves to the split line return-1
int templine = pointtoposition (0, Movey);
We deal with him
if (templine!= invalid_position) {
//As long as you don't move to the split line before I handle
mendposition = templine;
}
Drag-and-drop scrolling, rolling speed
int scrollspeed = 0;
Roll up
if (Movey < Upscroll) {
scrollspeed = ten;
Roll down
} else if (Movey > Downscroll) {
scrollspeed = -10;
}
Start scrolling
if (scrollspeed!= 0) {
//Compute the y
-coordinate int dragitemy = Getchildat (Mendposition- Getfirstvisibleposition ()). GetTop ();
Current speed
int dy = dragitemy + scrollspeed;
Set
setselectionfromtop (mendposition, dy);
}
When you're done moving, I can stop your window.
/**
* Stop position
/private void Stopdrag () {
//directly remove form
if (Dragimageview!= null) {
Wm.removeview (Dragimageview);
Dragimageview = null;
}
}
In this way, I will directly splice your position to achieve a drag-and-drop effect
/**
* Final Start Imaging
*
* @param upy
/
private void Ondrag (int upy) {
//Split Line processing
//judgment move to Split line return-1
int templine = pointtoposition (0, upy);
We deal with him
if (templine!= invalid_position) {
//As long as you don't move to the split line before I handle
mendposition = templine;
}
/**
* You fall directly at the top of the first and directly at the bottom
.///upper boundary treatment
if (Upy < Getchildat (1). GetTop ()) {
mendposition = 1;
Under boundary processing
} else if (Upy > Getchildat (Getchildcount ()-1). GetTop ()) {
mendposition = Getadapter (). GetCount () -1;
}
Start Updating item order
if (mendposition > 0 && mendposition < Getadapter (). GetCount ()) {
Dragadapter adapter = (Dragadapter) getadapter ();
Delete the original entry
Adapter.remove (Adapter.getitem (mstartposition));
Update
Adapter.insert (Adapter.getitem (mstartposition), mendposition);
}
All the code is posted
Draglistview
Package Com.liuguilin.draglistviewsample.view; * * Project Name: Draglistviewsample * Package Name: Com.liuguilin.draglistviewsample.view * FileName: Draglistview * Creator: LGL * Date Created: 2016/8/29
20:50 * Description: Custom high imitation QQ list can be dragged ListView * * Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.PixelFormat;
Import Android.util.AttributeSet;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.WindowManager;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import COM.LIUGUILIN.DRAGLISTVIEWSAMPLE.R;
Import Com.liuguilin.draglistviewsample.adapter.DragAdapter; public class Draglistview extends ListView {//press the selected position private int mstartposition;//need to reach position private int MENDP
Osition;
The relative y-coordinate of the finger in the entry is private int dragpoint;
ListView in the screen of the y-coordinate private int dragoffset;
on private int upscroll;
under private int downscroll;
Form private WindowManager WM; Screenshot of display private ImageView Dragimageview
Form parameters Private Windowmanager.layoutparams lparams; Construction method Public Draglistview (context, AttributeSet attrs) {Super (context, attrs);}/** * Get the location of the contact's entry * Get a picture of the selected entry * Event interception Mechanism * * @param EV * @return/@Override public boolean onintercepttouchevent (motionevent ev) {//Identify action switch (ev.get Action ()) {case Motionevent.action_down://get the coordinate int x = (int) ev.getx () of the contact, int y = (int) ev.gety ();//So you can calculate which entry I pressed into M
StartPosition = Mendposition = Pointtoposition (x, y);
Determine if the contact point is in the area of the logo viewgroup Itemview = (viewgroup) getchildat (Mstartposition-getfirstvisibleposition ());
Record the relative y-coordinate of the finger in the entry Dragpoint = Y-itemview.gettop ();
ListView the y-coordinate in the screen dragoffset = (int) (Ev.getrawy ()-y);
Drag icon View Dragger = Itemview.findviewbyid (R.id.iv_logo); Determine if the contact point is in the logo area if (dragger!= null && x < Dragger.getright () + 10) {//define ListView Scroll Entry//Up Upscroll = Getheigh
T ()/3;
Lower downscroll = getheight () * 2/3;
Gets the selected picture/screenshot itemview.setdrawingcacheenabled (true); Get screenshots Bitmap Bitmap = ITEMVIew.getdrawingcache ();
Picture scrolling StartDrag (BitMap, y);
} break;
//will also pass the event to the child view return false; /** * picture on Y axis, that is, up and down can be scrolled * * @param bitMap * @param y/private void StartDrag (BitMap bitMap, int y) {//form in imitation of wm = (WINDOWM
Anager) GetContext (). Getsystemservice (Context.window_service);
Set form parameter Lparams = new Windowmanager.layoutparams ();
Set width High lparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
Lparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Attribute Lparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Set semitransparent Lparams.alpha = 0.8f;
Set Center lparams.gravity = gravity.top;
Set XY lparams.x = 0;
Lparams.y = Y-dragpoint + dragoffset;
attribute Lparams.format = pixelformat.translucent;
Set animation lparams.windowanimations = 0;
Picture Dragimageview = new ImageView (GetContext ());
Set screenshot dragimageview.setimagebitmap (BITMAP);
Add Display Form Wm.addview (Dragimageview, lparams); /** * Touch Event * * @param EV * @rEturn/@Override public boolean ontouchevent (motionevent ev) {//Wrong position if (Dragimageview!= null && mendposition != invalid_position) {/////Sliding switch (ev.getaction ()) {case Motionevent.action_move://get directly to Y coordinate to move int movey =
(int) ev.gety ();
Dodrag (Movey);
Break
Stop dragging the imaging case MotionEvent.ACTION_UP:int upy = (int) ev.gety ();
Stopdrag ();
Ondrag (Upy);
Break
}//Intercept to event return true; /** * Final Start imaging * * @param upy/private void Ondrag (int upy) {//Split line processing//judgment move to Split line return-1 int templine = pointtoposition (0,
Upy);
We deal with him if (Templine!= invalid_position) {//As long as you do not move to the dividing line before I deal with mendposition = templine;}/** * You are directly at the top of the first and directly below the bottom of a/ Upper boundary processing if (Upy < Getchildat (1). GetTop ()) {mendposition = 1;//under Boundary processing} else if (Upy > Getchildat (Getchildcount ()- 1). GetTop ()) {mendposition = Getadapter (). GetCount ()-1;//start Updating item order if (Mendposition > 0 && mendposition < Getadapter (). GetCount ()) {Dragadapter adapter = (dragadapter) getadapter ();//delete original entry adapter.Remove (Adapter.getitem (mstartposition));
Update Adapter.insert (Adapter.getitem (mstartposition), mendposition); }/** * Stop position */private void Stopdrag () {//Directly remove form if (Dragimageview!= null) {Wm.removeview (dragimageview); dragimage
View = null; /** * Control Form Move * * @param movey * * private void Dodrag (int movey) {if (Dragimageview!= null) {Lparams.y = Movey-dra
Gpoint + dragoffset;
Wm.updateviewlayout (Dragimageview, lparams);
}//Judge move to split line return-1 int templine = pointtoposition (0, Movey);
We deal with him if (Templine!= invalid_position) {//As long as you don't move to the split line before I handle mendposition = Templine;}//drag-scrolling, scrolling speed int scrollspeed = 0; Roll up if (Movey < upscroll) {scrollspeed = 10;//Roll down} else if (Movey > Downscroll) {scrollspeed =-10;}//Start scrolling if (scrollspeed!= 0) {//The y-coordinate of the calculated entry is int dragitemy = Getchildat (Mendposition-getfirstvisibleposition ()). GetTop ();//current speed int dy = Dragitemy + sc
Rollspeed;
Set Setselectionfromtop (mendposition, DY); }
}
}
And then we quote
Layout_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:layout_width=" match_parent "
android:orientation=" vertical "
android:layout" _height= "Match_parent" >
<com.liuguilin.draglistviewsample.view.draglistview
android:id= "@+id/ Mlistview "
android:layout_width=" match_parent "
android:layout_height=" match_parent "/>
</ Linearlayout>
Right, don't forget the permission to add a form OH
<!--window permissions-->
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
After doing this series of things, I think this idea is actually right, and then we can try to run
The above is a small set to introduce the Android custom ListView to achieve imitation QQ can drag and drop list function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!