In the QQ group, many friends asked how to implement the effect of sliding the listview in the notice bar of android4.0 to delete an item. Here I have simply implemented it. Let's see the figure (a picture is better than a thousand words ).
Slice
Implementation
Idea: 1. Add a sliding listening event to the listview (obtain the sliding position in the event to obtain the listview item ).
2. Let this item play an animation (sliding from left to right)
3. After the item animation is played, delete the item
Code details
The Code is as follows: MainActivity. java :( PS: Very simple, there is no technical content, the old bird flew over, Do not spray .)
[Java]
Package com. yangfuhai. animation1;
Import java. util. ArrayList;
Import android. app. ListActivity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;
Import android. view. animation. Animation;
Import android. view. animation. Animation. AnimationListener;
Import android. view. animation. AnimationUtils;
Import android. widget. AdapterView;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
Import android. widget. Toast;
/**
* @ Title imitates the notification bar animation of android 4.0
* @ Description listview slide to delete an item
* @ Company explorer Network Studio (www.tsz.net)
* @ Author michael Young (www.YangFuhai.com)
* @ Version 1.0
* @ Created 2012-9-29
*/
Public class MainActivity extends ListActivity {
Private ArrayList <String> array;
Private ArrayAdapter <String> adapter;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
ListView listView = getListView ();
Array = new ArrayList <String> ();
String aa [] = {"items1", "item2", "items3", "item4", "items5 ",
"Item6", "items7", "item8", "items9", "item10", "items11 ",
"Item12 "};
For (int I = 0; I <aa. length; I ++ ){
Array. add (aa [I]);
}
Adapter = new ArrayAdapter <String> (this, android. R. layout. simple_list_item_1, array );
ListView. setAdapter (adapter );
/**
* Add a listview sliding answer
*/
ListView. setOnTouchListener (new OnTouchListener (){
Float x, y, upx, upy;
Public boolean onTouch (View view, MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
X = event. getX ();
Y = event. getY ();
}
If (event. getAction () = MotionEvent. ACTION_UP ){
Upx = event. getX ();
Upy = event. getY ();
Int position1 = (ListView) view). pointToPosition (int) x, (int) y );
Int position2 = (ListView) view). pointToPosition (int) upx, (int) upy );
If (position1 = position2 & Math. abs (x-upx)> 10 ){
View v = (ListView) view). getChildAt (position1 );
RemoveListItem (v, position1 );
}
}
Return false;
}
});
/**
* Listview item Click Event
*/
ListView. setOnItemClickListener (new AdapterView. OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Parent, View rowView, int positon, long id ){
Toast. makeText (rowView. getContext (), "you clicked item" + positon + "location", Toast. LENGTH_SHORT). show ();
// RemoveListItem (rowView, positon );
}
});
}
/**
* Delete an item and play the animation.
* @ Param rowView: view of the animation to be played
* @ Param positon location of the item to be deleted
*/
Protected void removeListItem (View rowView, final int positon ){
Final Animation animation = (Animation) AnimationUtils. loadAnimation (rowView. getContext (), R. anim. item_anim );
Animation. setAnimationListener (new AnimationListener (){
Public void onAnimationStart (Animation animation ){}
Public void onAnimationRepeat (Animation animation ){}
Public void onAnimationEnd (Animation animation ){
Array. remove (positon );
Adapter. notifyDataSetChanged ();
Animation. cancel ();
}
});
RowView. startAnimation (animation );
}
}
Animation file item_anim.xml:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Translate xmlns: android = "http://schemas.android.com/apk/res/android"
Android: duration= "800"
Android: fromXDelta = "0"
Android: fromYDelta = "0"
Android: toXDelta = "800"
Android: toYDelta = "0"/>