Android ListView deletes an animation and androidlistview

Source: Internet
Author: User

Android ListView deletes an animation and androidlistview

When the Android ListView is deleted, the deleted items directly disappear and are relatively stiff. Here we will discuss how to delete the animation;

The following is the Activity code. There is only one ListView on the main interface that provides data through the Adapter. The interface is as follows. When you click an item, 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 {
/**
* The Adapter provides data and the remove Method to delete specified entries;
*
**/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. remove (position) ;}notifydatasetchanged () ;}@ Override public int getCount () {Return mItems. size () ;}@ Override public int getItemViewType (int position) {// current menu type return position ;}@ 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, ViewGro Up parent) {if (convertView = 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); setCon TentView (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 ){
// Obtain the position int firstVisiblePosition = mListView. getFirstVisiblePosition () of the first View of ListView ();
// Store all the Animator and use the AnimatorSet to directly play the ArrayList <Animator> animators = new ArrayList <Animator> ();
// Obtain the View itemToDelete = mListView to be deleted. getChildAt (position-firstVisiblePosition); 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 ends, restore the attributes of all subviews of the ListView 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 action 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 will // 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 );}}

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.