ListView Long Press the Help class to drag the item Exchange location

Source: Internet
Author: User
Tags gety

Abstract class encapsulation, you need to create a helper class object in activity and override the method Changeitemposition (id1 in the data source, ID2 in the data source), and the method to re-display the ListView Resetlistview


Key steps:

1.listview after normal display, create a new Lmxlistviewhelper object and pass in the ListView object, overriding two abstract methods

2. Turn on the item Exchange function --helper.enablechangeitems (true);


effect, long pressing an item, the entry becomes translucent, and when you drag the item to another entry, the entry begins to blink, lifting your finger, and swapping the dragged item with the flashing item position.

Flashing animation at the beginning did not write, is the online source, the specific source forgot

In addition, there is no cross-screen drag-and-drop function (two item distance beyond the screen length will not be exchanged), and later will be added

Hope to help everyone, the wrong place still look correct






Help Class Code:



Package com.example.androidtest;


Import android.app.Activity;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnTouchListener;
Import android.view.animation.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.LinearInterpolator;
Import android.view.animation.TranslateAnimation;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemLongClickListener;
Import Android.widget.ListView;


Public abstract class Lmxlistviewhelper {


ListView LV;


Position1: The ID of the item being dragged
public int position1 =-1;
The view of the dragged item
Public View View1;
Public View View2;
The original y-coordinate of the item being moved
public float y1 =-1;


Abstract method Exchanges the location of two item in a ListView
public abstract void changeitemposition (int p1, int p2);


Abstract Method Recharge ListView
public abstract void Resetlistview ();


Public Lmxlistviewhelper (ListView LV) {
Super ();
this.lv = LV;
}


/**
*
* @Title: Enablechangeitems
* @Description: TODO (whether to enable long press to swap the item location)
* @param @param b true--> enabled,false--> disabled
* @return void return type
* @throws
*/
public void Enablechangeitems (Boolean b) {
if (!b) {
Return
}


After long pressing, determine the ID of the item1 that needs to be exchanged (Position1), determine the Item1 view
Lv.setonitemlongclicklistener (New Onitemlongclicklistener () {


@Override
public Boolean Onitemlongclick (adapterview<?> parent, view view,
int position, long ID) {
TODO auto-generated Method Stub
Position1 = position;
Y1 = Lv.getchildat (position). GetY ();
Lv.getchildat (position). Setalpha (0.5f);
return false;
}
});


Ontouch Monitoring
/*
* ACTION_MOVE:1,ITEM1 's view follows the finger up and down 2, real-time monitor which item is currently to be replaced, the corresponding item display animation effect
*/
Lv.setontouchlistener (New Ontouchlistener () {


@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
Switch (event.getaction ()) {
Case Motionevent.action_move:
if (Position1 = =-1) {
Position1==-1 true to indicate that no long press on an item
Break
}
Item1 view follows your finger up and down
Lv.getchildat (Position1). Sety (Event.gety ());
Detect the item that is currently covered
CHECKITEM2 (Lv.getchildat (Position1). GetY ());
Break


Case MOTIONEVENT.ACTION_UP:
Replaceitems (Event.gety ());
Break


Default
Break
}
return false;
}
});


}


protected void checkItem2 (float y) {
TODO auto-generated Method Stub
for (int i = 0; i < Lv.getchildcount (); i++) {
LOG.I ("res", "Release replacement item" + i);
if (Y > Lv.getchildat (i). GetTop ()
&& y < Lv.getchildat (i). Getbottom ()) {
The item id=i is currently overwritten
Startflick (Lv.getchildat (i));
} else {
Lv.getchildat (i). Clearanimation ();
}
}
}


public void Replaceitems (float y) {
if (y1 = =-1) {
Y1 not assigned value Description Onitemlongclick not assigned to Y1 no item is long pressed
LOG.I ("res", "No item is long pressed");
Return
}
for (int i = 0; i < Lv.getchildcount (); i++) {
Missed this sentence for more than a day, remember to clear the animation!!!
Lv.getchildat (i). Clearanimation ();
if (Y > Lv.getchildat (i). GetTop ()
&& y < Lv.getchildat (i). Getbottom ()) {
The item that is covered when you lift your finger and the long press item Exchange location
LOG.I ("res", "action_up substituting subscript" + i);
Changeitemposition (Position1, i);
Position1 =-1;
y1 =-1;
Return
}
}
LOG.I ("res", "action_up not conforming to item");
If the traversal completes without finding the item you want to replace View1 move back to its original position
Clearparams ();
}


/*
* No Exchange item, clear various data
*/
public void Clearparams () {
Translateanimation translateanimation = new Translateanimation (LV
. Getchildat (Position1). GetX (), Lv.getchildat (Position1). GetX (),
Lv.getchildat (Position1). GetY (), y1);
Translateanimation.setfillafter (FALSE);
Translateanimation.setduration (400);
Lv.getchildat (Position1). Startanimation (translateanimation);
Resetlistview ();
If Position1 is the regular element of the ListView (not Header/footer)
if (Position1 < Lv.getadapter (). GetCount ()
&& Lv.getchildat (position1)! = null) {
Lv.getchildat (Position1). Clearanimation ();
}
y1 =-1;
Position1 =-1;
Resetlistview ();
}


/*
* Flashing Animation
*/
private void Startflick (view view) {


if (null = = view) {


Return


}
if (view.getanimation () = = null) {
Animation alphaanimation = new Alphaanimation (1, 0);


Alphaanimation.setduration (200);


Alphaanimation.setinterpolator (New Linearinterpolator ());


Alphaanimation.setrepeatcount (Animation.infinite);


Alphaanimation.setrepeatmode (Animation.reverse);


View.startanimation (alphaanimation);
}
}


/**
*
* Remove the view flicker effect
*
*
*
* */


private void Stopflick (view view) {


if (null = = view) {


Return


}


View.clearanimation ();


}
}



How to use in activity:

Package com.example.androidtest;


Import java.util.ArrayList;
Import Java.util.HashMap;


Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;


public class Mainactivity extends Activity {


ListView LV;
ArraylistSimpleadapter adapter;
Button bn;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);


/*
* Set ListView Display Data
*/
LV = (ListView) Findviewbyid (r.id.lv);
DataList = new arraylistDataList = Getsortdata ();
adapter = new Simpleadapter (Mainactivity.this, DataList,
R.layout.item_sort_management_listview,
New string[] {"Sortname"},
New int[] {r.id.tv_sortmanagement_lv});
Lv.setadapter (adapter);


Lmxlistviewhelper helper = new Lmxlistviewhelper (LV) {


@Override
public void Resetlistview () {
TODO auto-generated Method Stub
ListView Re-display
adapter = new Simpleadapter (Mainactivity.this, DataList,
R.layout.item_sort_management_listview,
New string[] {"Sortname"},
New int[] {r.id.tv_sortmanagement_lv});
Lv.setadapter (adapter);
}


@Override
public void changeitemposition (int p1, int p2) {
TODO auto-generated Method Stub
Complete the replacement of the two item with ID p1 and P2 in the data source of the ListView according to the type of your ListView
hashmap<string, string> tempmap = Datalist.get (p2);
Datalist.set (P2, Datalist.get (p1));
Datalist.set (P1, Tempmap);
adapter = new Simpleadapter (Mainactivity.this, DataList,
R.layout.item_sort_management_listview,
New string[] {"Sortname"},
New int[] {r.id.tv_sortmanagement_lv});
Lv.setadapter (adapter);
}
};


Helper.enablechangeitems (TRUE);


}


/*
* Exchange Item
*/
public void changeitemposition (int p1, int p2) {
TODO auto-generated Method Stub
LOG.I ("res", "two of the swap positions are p1-->" + p1 + "\tp2-->" + p2);
hashmap<string, string> tempmap = new hashmap<string, string> ();
Tempmap = Datalist.get (p2);
Datalist.set (P2, Datalist.get (p1));
Datalist.set (P1, Tempmap);
adapter = new Simpleadapter (Mainactivity.this, DataList,
R.layout.item_sort_management_listview,
New string[] {"Sortname"},
New int[] {r.id.tv_sortmanagement_lv});
Lv.setadapter (NULL);
LOG.I ("res", "Changeitemposition swap position execution");
for (int i = 0; i < datalist.size (); i++) {
LOG.I ("res",
"Item" + i + "\tcontent:" + datalist.get (i). Get ("Sortname"));
}
Lv.setadapter (adapter);
}


/*
* Initialize LV
*/
Public arraylistarraylistHashmap<string, string> map;
Map = new hashmap<string, string> ();
Map.put ("Sortname", "homemade stir-fry");
Sortlist.add (map);
Map = new hashmap<string, string> ();
Map.put ("Sortname", "classic Sichuan");
Sortlist.add (map);
Map = new hashmap<string, string> ();
Map.put ("Sortname", "Crock Stew Soup");
Sortlist.add (map);
Map = new hashmap<string, string> ();
Map.put ("Sortname", "Pasta baked rice");
Sortlist.add (map);
Map = new hashmap<string, string> ();
Map.put ("Sortname", "fine snacks");
Sortlist.add (map);
return sortlist;


}
}





ListView Long Press the Help class to drag the item Exchange location

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.