Android ListView Delete Animation

Source: Internet
Author: User

Android ListView when deleted, deleted items directly disappear, more blunt, in this implementation of delete animation, we discuss together;

The following is the activity code; The main interface has only one ListView, which provides the data via adapter, the interface is as follows, when an item is clicked, the animation is played and the clicked item is deleted;

Package App.com.listviewdeleteanimation;import Android.animation.animator;import Android.animation.AnimatorSet; Import Android.animation.objectanimator;import Android.content.context;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.ViewGroup;import Android.view.animation.acceleratedecelerateinterpolator;import Android.widget.adapterview;import Android.widget.arrayadapter;import Android.widget.baseadapter;import Android.widget.listview;import Android.widget.textview;import Java.util.arraylist;public class Mainactivity extends Actionbaractivity implements listview.onitemclicklistener{
/**
* Adapter provides data, provides the Remove method, can delete the specified entry;
*
**/public class Adapter extends baseadapter{private Context mcontext; Private arraylist<string> Mitems; Public Adapter (Context c,string [] data) {super (); Mcontext = C; Mitems = new arraylist<string> (); for (String item:data) {mitems.add (item); }} public void remove (int position) {if (Position < Mitems.size ()) {Mitems.remo ve (position); } notifydatasetchanged (); } @Override public int getcount () {return mitems.size (); } @Override public int getitemviewtype (int. position) {//Current menu type return pos ition; } @Override public String getItem (int position) {return mitems.get (position); } @Override public Boolean isEmpty () {return mitems.isempty (); } @OverRide public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {if (CONVERTVI EW = = NULL) {Convertview = Layoutinflater.from (Mcontext). Inflate (Android. R.layout.simple_list_item_1, parent, false); } TextView TV = (TextView) Convertview; Tv.settext (GetItem (position)); return convertview; }} private ListView Mlistview; Private Adapter Madapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mlistview = (ListView) Findviewbyid (R.id.listview); Madapter = new Adapter (this, new string[] {"1", "2", "3", "4", "5", "6", "7",}); Mlistview.setadapter (Madapter); Mlistview.setonitemclicklistener (this); } @Override public void ONITEMCLIck (adapterview<?> Parent, view view, final int position, long ID) {
Get the listview position int firstvisibleposition = Mlistview.getfirstvisibleposition () for the first view;
Store all Animator, using Animatorset direct playback arraylist<animator> animators = new arraylist<animator> ();
     Get the view view Itemtodelete = Mlistview.getchildat (position-firstvisibleposition) you want to delete; int viewheight = Itemtodelete.getheight (); int dividerheight = Mlistview.getdividerheight (); Objectanimator hideanimator = objectanimator.offloat (Itemtodelete, "Alpha", 1f, 0f); Animators.add (Hideanimator); int delay = 0; int firstviewtomove = position + 1; for (int i=firstviewtomove;i < Mlistview.getchildcount (); ++i) {View viewtomove = Mlistview.getchildat (i); Objectanimator moveanimator = objectanimator.offloat (Viewtomove, "Translationy", 0,-dividerheight-viewheight); Moveanimator.setinterpolator (New Acceleratedecelerateinterpolator ()); Moveanimator.setstartdelay (delay); Delay + = 100; Animators.add (Moveanimator); } animatorset set = new Animatorset (); Set.addlistener (New Animator.animatorlistener () {@Override public void OnANimationstart (Animator animation) {} @Override public void Onanimationend (Animator animation) { Madapter.remove (position); After the animation is finished, restore the properties for the ListView All Child view for (int i=0;i<mlistview.getchildcount (); ++i) {View v = Mlistview.getchildat (i); V.setalpha (1f); V.settranslationy (0); }} @Override public void Onanimationcancel (Animator animation) {} @Override public void Onanimationrepeat (Animator animation) {}}); Set.playtogether (animators); Set.start (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar Item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml. int id = item.getitemid (); Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); }}

Android ListView Delete animation

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.