Imitating the notice bar of android4.0, listview slides to delete items, with a sliding animation.

Source: Internet
Author: User

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"/>

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.