Android View about transient

Source: Internet
Author: User

Let's look at the delete animation for the ListView today.

As the ListView scrolls, the item on the screen is reused to display different data

This will cause us to be removed to the wrong item

Or the problem with the display (the item shows that the data is different but the animation is still running on it)

To solve this problem, use the View category in API 16 to add a new method


public void Sethastransientstate (Boolean hastransientstate)

When set to true, tell the system that the View should be kept as far as possible until sethastransientstate (false) is called

Note that this can be set repeatedly, meaning that each sethastransientstate (true) is paired with a sethastransientstate (false) to reply

The contents of the Listview Onitemclicklistener//This example points out the item and then fades out and deletes the itempublic void Onitemclick (Adapterview   parent, Final View view, int position, long ID) {    final String item = (String) parent.getitematposition (position);    Objectanimator Anim = objectanimator.offloat (view, View.alpha, 0);    Anim.setduration (+);    View.sethastransientstate (TRUE); Set to True to declare item to be traced    Anim.addlistener (new Animatorlisteneradapter () {        @Override public        Void Onanimationend (Animator animation) {            mylistview.remove (item);            Adapter.notifydatasetchanged (); Re-organize the ListView            View.setalpha (1);            View.sethastransientstate (FALSE); Set back to False} when finished    );    Anim.start ();}

Another way is to use viewpropertyanimator to solve

public void Onitemclick (Adapterview   Parent, Final view view, int position, long ID) {    final String item = (String) Parent.getitematposition (position);    View.animate (). Setduration (+).        Alpha (0)        . Withendaction (New Runnable () {            @Override            public void Run () {                mylistview.remove (item);                Adapter.notifydatasetchanged ();                View.setalpha (1);}}    );

But the biggest problem with these two approaches is that API 16 will be used later (with Withendaction and sethastransientstate)

(Damn Android fragmentation ...)

Let's look at another example

In Devbytes:animating ListView Deletion:now on gingerbread! In

He used a custom adapter that inherits from Arrayadapter.

Override the public boolean hasstableids () so that it always callbacks true to replace sethastransientstate (true)

Used to replace sethastransientstate (True) @Overridepublic Boolean hasstableids () {    return true;//Always callback true to replace Sethastransientstate (True)}

And the withendaction part is replaced with Setanimationlistener.

Methods prior to API 11 translateanimation translator = new Translateanimation (StartX, EndX, Starty, EndY); translator.setduration View.startanimation (translator); View.getanimation (). Setanimationlistener (New Animationlisteneradapter () {    @Override public    void Onanimationend (Animation Animation) {        new Runnable () {            @Override            public void Run () {                //... (slightly)}}}    );

This allows gingerbread (API 10) or earlier versions of Android to be compatible

If this is the version after 4.0 (API 14)

Or do it with view.animate () and objectanimator?

API is less than 16 but greater than or equal to 14 can do View.animate (). setduration, Objectanimator anim = objectanimator.offloat (view, View.translation_x, StartX, EndX); anim.setduration, Anim.start (); Setanimatorendaction (Anim, New Runnable () {    @Override public    void Run () {        //... (abbreviated)    }});

Android View about transient

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.