Android implements the item free drag effect in the GridView _android

Source: Internet
Author: User
Tags abstract addall

Before the work, the need to implement a function is the GridView in the item can be free to drag, think about it, in fact, not very difficult to achieve, the main work is to exchange nodes, as well as the moving effect of the drag, the following talk about the specific implementation:

First declare a baseadapter:

Package Com.dafasoft.dragablegridview;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AbsListView;
Import Android.widget.AdapterView;
Import Android.widget.BaseAdapter;
Import java.util.ArrayList;
Import java.util.List;
/** * Created by Zhangyulong on 16/9/22. * * Public abstract class Basedragableadapter<t> extends Baseadapter {protected list<t> mlist; protected Conte
XT Mcontext;
protected Layoutinflater Minflater;
Initialize position information to hide protected int mhideposition = adapterview.invalid_position;
Public Basedragableadapter {this.mcontext = context, This.minflater = layoutinflater.from (context);
Mlist = new arraylist<t> (); @Override public int GetCount () {return mlist = = null 0:mlist.size ();} @Override public Object getitem (int i) {RE Turn mlist = = null?
0:mlist.get (i); @Override public long Getitemid (int i) {return I.} @Override public abstract View GetView (int i, view view, ViewGroup ViewGroup); 
public void setlist (list<t> list) {if (this.mlist!= null) this.mList.clear ();//Avoid dirty data if (List = = null) {return;
} this.mlist = list; public void setlist (t[] list) {arraylist<t> ArrayList = new Arraylist<t> (list.length), for (T t:list) {AR
Raylist.add (t);
} setlist (arrayList); Public list<t> GetList () {return this.mlist} is public void AddAll (list<t> list) {if (list = = NULL | | | list.
Size () = = 0) return;
This.mList.addAll (list); public void AddAll (int location, list<t> list) {if (list = = NULL | | list.size () = 0) return; THIS.MLIST.ADDALL (l
Ocation, list); public void Clean () {if (GetCount () = 0) return; Mlist.clear ();} public void Hideview (int position) {mhideposition =
Position
Notifydatasetchanged (); public void Showhideview () {//reset hideposition mhideposition = adapterview.invalid_position; notifydatasetchanged ();}/ * * Swap node * @param draggedpos Drag the start node * @param currentpos dragThe current node */public void Swapview (int draggedpos, int currentpos) {//From after go to drag if (Draggedpos < Currentpos) {///To move the dragged node to the current node
Mlist.add (Currentpos + 1, mlist.get (Draggedpos));
Remove the node before dragging Mlist.remove (Draggedpos); }//from back to forward else if (Draggedpos > Currentpos) {//Move the dragged node to the current node Mlist.add (Currentpos, Mlist.get (Draggedpos));//delete the section before dragging
Point Mlist.remove (Draggedpos + 1);
} mhideposition = Currentpos;
Notifydatasetchanged (); }
}

Second implementation Dragablegridview:

Package Com.dafasoft.dragablegridview;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.PixelFormat;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.widget.AdapterView;
Import Android.widget.GridView;
Import Android.widget.ImageView;
/** * Created by Zhangyulong on 16/9/22. * * public class Dragablegridview extends GridView {private static final int drag_img_show = 1; private static final int D
rag_img_not_show = 0;
private static final String Log_tag = "Draggridview";
Private static final float amp_factor = 1.2f;
Private ImageView Dragimageview;
Private Windowmanager.layoutparams Dragimageviewparams;
Private WindowManager WindowManager;
Private Boolean Isviewondrag = false;
/**previous dragged over position*/private int predraggedoverpositon = adapterview.invalid_position;
private int downrawx; Private int downrawy; Public Dragablegridview {Super [context]; Initview ();} public Dragablegridview AttributeSet attrs) {Super (context, attrs); Initview ();} public Dragablegridview (context, AttributeSet attrs, in T defstyleattr) {Super (context, attrs, defstyleattr); Initview ();} public void Initview () {Setonitemlongclicklistener (M
Onitemlongclicklistener);
Setonitemclicklistener (Onitemclicklistener);
Setonitemselectedlistener (Onitemselectedlistener);
Initializes the image view Dragimageview = new ImageView (GetContext ()) of the dragged item.
Dragimageview.settag (drag_img_not_show);
Initializes the parameter object used to set the Dragimageview dragimageviewparams = new Windowmanager.layoutparams (); Gets the window management object that is used to add dragimageview WindowManager = (WindowManager) getcontext () to the rear-facing window. Getsystemservice (context.window_
SERVICE); Private Onitemlongclicklistener Monitemlongclicklistener = new Onitemlongclicklistener () {@Override public boolean OnI Temlongclick (adapterview<?> Parent, view view, int POSition, long ID) {//record length by item position Predraggedoverpositon = position;//Get the Drawing cache View.destroydrawingcache () with the item long pressed; v
Iew.setdrawingcacheenabled (TRUE);
Gets the Bitmap Bitmap dragbitmap = Bitmap.createbitmap (View.getdrawingcache ()) that drags the item by being long pressed item; Set parameters for dragging item dragimageviewparams.gravity = Gravity.top |
Gravity.left;
Set the drag item to the original item 1.2 times times Dragimageviewparams.width = (int) (Amp_factor*dragbitmap.getwidth ());
Dragimageviewparams.height = (int) (Amp_factor*dragbitmap.getheight ());
Set the touch point to draw the center dragimageviewparams.x = (DOWNRAWX-DRAGIMAGEVIEWPARAMS.WIDTH/2) of the dragged item;
Dragimageviewparams.y = (DOWNRAWY-DRAGIMAGEVIEWPARAMS.HEIGHT/2); Dragimageviewparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
Set window to support transparency Dragimageviewparams.format = pixelformat.translucent;
dragimageviewparams.windowanimations = 0; Dragimageview is the container for the item being dragged, emptying the previous displayShow if (int) dragimageview.gettag () = = Drag_img_show) {Windowmanager.removeview (Dragimageview); Dragimageview.settag (
Drag_img_not_show);
//Set the item Dragimageview.setimagebitmap (DRAGBITMAP) that was long pressed;
Add Drag item to screen Windowmanager.addview (Dragimageview, dragimageviewparams);
Dragimageview.settag (drag_img_show);
Isviewondrag = true;
The setting is not displayed ((Basedragableadapter) Getadapter ()) by the item is long (()). Hideview (position);
return true;
}
}; @Override public boolean ontouchevent (motionevent ev) {//When pressed, record the pressed coordinates if (ev.getaction () = = Motionevent.action_down) {//obtained
The coordinate downrawx = (int) ev.getrawx () of the touch point relative to the screen;
Downrawy = (int) Ev.getrawy (); When//dragimageview is being dragged, update dragimageview position else if (ev.getaction () = = Motionevent.action_move) && (
Isviewondrag = = True) {log.i (Log_tag, "" + ev.getrawx () + "" + Ev.getrawy ());
Set the touch point to Dragimageview center dragimageviewparams.x = (int) (EV.GETRAWX ()-Dragimageview.getwidth ()/2);
DRAGIMAGEVIEWPARAMS.Y = (int) (Ev.getrawy ()-Dragimageview.getheight ()/2); Update window display, move Dragimageview position WindOwmanager.updateviewlayout (Dragimageview, dragimageviewparams);
Gets the item position int currdraggedposition = pointtoposition (int) ev.getx (), (int) ev.gety ()) for the current touch point; If the current stay position item does not equal the item for the last stay, swap the item if (currdraggedposition!= adapterview.invalid_position) && ( Currdraggedposition!= Predraggedoverpositon)) {(Basedragableadapter) Getadapter ()). Swapview (
Predraggedoverpositon, currdraggedposition);
Predraggedoverpositon = currdraggedposition; }//Free Dragimageview else if ((ev.getaction () = = motionevent.action_up) && (Isviewondrag = = True)) {(basedragabl
Eadapter) Getadapter ()). Showhideview (); if (int) dragimageview.gettag () = = Drag_img_show) {Windowmanager.removeview (Dragimageview); Dragimageview.settag (
Drag_img_not_show);
} Isviewondrag = false;
return super.ontouchevent (EV); }
}

The

implements a adapter inheritance from Basedragableadapter:

Package Com.dafasoft.dragablegridview;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.TextView;
/**
* Created by Zhangyulong on 16/9/22.
*
/public class Myadapter extends basedragableadapter<string> {public
myadapter (context) {
super (context);
}
@Override public
View getview (int i, view view, ViewGroup viewgroup) {
viewholder viewholder;
if (view = = null) {
Viewholder = new Viewholder ();
View = Minflater.inflate (r.layout.adapter_my, null);
VIEWHOLDER.MITEMTV = (TextView) View.findviewbyid (r.id.tv_item);
View.settag (Viewholder);
} else {
Viewholder = (viewholder) view.gettag ();
}
if (mhideposition = i) {
view.setvisibility (view.gone);
} else {
view.setvisibility (view.visible);
}
ViewHolder.mItemTv.setText (Mlist.get (i));
return view;
}
Class viewholder{
TextView mitemtv
}
}

Used in activity:

Package Com.dafasoft.dragablegridview;
Import android.app.Activity;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import java.util.ArrayList;
Import java.util.List;
public class Mainactivity extends activity {
private dragablegridview MDRAGABLEGV;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
MDRAGABLEGV = (Dragablegridview) Findviewbyid (R.id.drag_grid_view);
Myadapter adapter = new Myadapter (this);
list<string> list = new arraylist<string> ();
for (int i = 0; i < i + +) {
List.add ("position" + i);
}
Adapter.setlist (list);
Mdragablegv.setadapter (adapter);
}

The above is a small set of Android to introduce the GridView of the item free drag effect, to achieve a simulation of the background data login effect, hope to help everyone, 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.