Library

Video Player is loading.
 
Current Time 0:00
Duration 7:16
Loaded: 0%
 

x1.00


Back

Games & Quizzes

Training Mode - Typing
Fill the gaps to the Lyric - Best method
Training Mode - Picking
Pick the correct word to fill in the gap
Fill In The Blank
Find the missing words in a sentence Requires 5 vocabulary annotations
Vocabulary Match
Match the words to the definitions Requires 10 vocabulary annotations

You may need to watch a part of the video to unlock quizzes

Don't forget to Sign In to save your points

Challenge Accomplished

PERFECT HITS +NaN
HITS +NaN
LONGEST STREAK +NaN
TOTAL +
- //

We couldn't find definitions for the word you were looking for.
Or maybe the current language is not supported

  • 00:04

    CHET HAASE: Hi, I'm Chet Haase from the Android team at
    CHET HAASE: Hi, I'm Chet Haase from the Android team at

  • 00:08

    Google, where I work on graphics and animations.
    Google, where I work on graphics and animations.

  • 00:10

    And today I wanted to talk a little bit more about ListView
    And today I wanted to talk a little bit more about ListView

  • 00:14

    animations, which I talked about in a previous episode.
    animations, which I talked about in a previous episode.

  • 00:17

    This is another episode in a series that I like to call,
    This is another episode in a series that I like to call,

  • 00:21

    You're Doing It Wrong.
    You're Doing It Wrong.

  • 00:22

    So this is another item related to the last one where
    So this is another item related to the last one where

  • 00:26

    there is a need to understand how views are
    there is a need to understand how views are

  • 00:29

    actually used by ListView.
    actually used by ListView.

  • 00:31

    ListView recycles views, which means that when those views
    ListView recycles views, which means that when those views

  • 00:34

    are not on the screen, it may use that view to hold
    are not on the screen, it may use that view to hold

  • 00:36

    different content.
    different content.

  • 00:37

    And you need to be aware of that, especially when you're
    And you need to be aware of that, especially when you're

  • 00:40

    running animations, so that you're not
    running animations, so that you're not

  • 00:41

    animating the wrong thing.
    animating the wrong thing.

  • 00:43

    So to demonstrate that, let's take a look at a little demo.
    So to demonstrate that, let's take a look at a little demo.

  • 00:46

    So we have this demo that I call ListView Animations.
    So we have this demo that I call ListView Animations.

  • 00:49

    And there's a long list, and quite a
    And there's a long list, and quite a

  • 00:52

    boring one, of cheeses.
    boring one, of cheeses.

  • 00:54

    Now, as I click on these items, they're going to
    Now, as I click on these items, they're going to

  • 00:56

    automatically fade out and then be
    automatically fade out and then be

  • 00:58

    removed from the adapter.
    removed from the adapter.

  • 00:59

    And the ListView is going to update itself, and that cheese
    And the ListView is going to update itself, and that cheese

  • 01:02

    will no longer exist.
    will no longer exist.

  • 01:03

    So we can scroll someplace.
    So we can scroll someplace.

  • 01:05

    We can delete this one.
    We can delete this one.

  • 01:06

    We can see the fadeout.
    We can see the fadeout.

  • 01:08

    And then the ListView collapses around it.
    And then the ListView collapses around it.

  • 01:10

    So all of that is good.
    So all of that is good.

  • 01:12

    But what happens if we delete a couple of these and then
    But what happens if we delete a couple of these and then

  • 01:15

    fling the list?
    fling the list?

  • 01:17

    Hopefully, you can see this on the screen.
    Hopefully, you can see this on the screen.

  • 01:19

    But what you should be able to see is, as I'm flinging the
    But what you should be able to see is, as I'm flinging the

  • 01:22

    list, there seem to be items that have nothing to do with
    list, there seem to be items that have nothing to do with

  • 01:25

    the ones that I clicked on that are being removed.
    the ones that I clicked on that are being removed.

  • 01:28

    And this is because we're running a fade animation on a
    And this is because we're running a fade animation on a

  • 01:30

    view, which has since been recycled and is being used for
    view, which has since been recycled and is being used for

  • 01:34

    different content.
    different content.

  • 01:35

    Now, that doesn't mean that the wrong
    Now, that doesn't mean that the wrong

  • 01:36

    item is being deleted.
    item is being deleted.

  • 01:37

    But it does mean that the wrong view is being animated.
    But it does mean that the wrong view is being animated.

  • 01:39

    So what happens is we click on an item.
    So what happens is we click on an item.

  • 01:42

    It starts running a fade animation on it.
    It starts running a fade animation on it.

  • 01:44

    We fling the list.
    We fling the list.

  • 01:45

    That view is off the screen.
    That view is off the screen.

  • 01:46

    It gets reused for different content.
    It gets reused for different content.

  • 01:48

    The view continues fading, because
    The view continues fading, because

  • 01:50

    it's the same instance.
    it's the same instance.

  • 01:51

    It's the same object inside.
    It's the same object inside.

  • 01:54

    So it continues fading, even though it now
    So it continues fading, even though it now

  • 01:56

    shows different content.
    shows different content.

  • 01:57

    And then when that animation is done, then we automatically
    And then when that animation is done, then we automatically

  • 02:00

    delete the original item.
    delete the original item.

  • 02:01

    So the data is still intact.
    So the data is still intact.

  • 02:03

    That's fine.
    That's fine.

  • 02:04

    But the visual experience on the screen is just weird.
    But the visual experience on the screen is just weird.

  • 02:07

    We don't want to do stuff like that.
    We don't want to do stuff like that.

  • 02:09

    So there's a couple of ways that you can account for this.
    So there's a couple of ways that you can account for this.

  • 02:11

    And I want to talk about those today.
    And I want to talk about those today.

  • 02:13

    So for one thing, you could use this item called View
    So for one thing, you could use this item called View

  • 02:17

    Property Animator.
    Property Animator.

  • 02:18

    And as of Jelly Bean-- actually, both of these ways
    And as of Jelly Bean-- actually, both of these ways

  • 02:21

    of compensating for this are in the Jelly Bean release.
    of compensating for this are in the Jelly Bean release.

  • 02:24

    One is to use View Property Animator, which internally
    One is to use View Property Animator, which internally

  • 02:26

    tells the system, hey, I'm not done with this view yet.
    tells the system, hey, I'm not done with this view yet.

  • 02:29

    So just let me run my animation in peace.
    So just let me run my animation in peace.

  • 02:32

    So if I'm using that for the animations and then I do the
    So if I'm using that for the animations and then I do the

  • 02:36

    fling operation, you'll see that there is no more
    fling operation, you'll see that there is no more

  • 02:39

    disruption on the screen.
    disruption on the screen.

  • 02:40

    Nothing is being faded out in the new views that we're
    Nothing is being faded out in the new views that we're

  • 02:44

    seeing on the screen, because we are persisting the views
    seeing on the screen, because we are persisting the views

  • 02:47

    that had the animations running on them.
    that had the animations running on them.

  • 02:49

    So, basically, we're grabbing it and saying, you know what?
    So, basically, we're grabbing it and saying, you know what?

  • 02:51

    I have a lock on this view.
    I have a lock on this view.

  • 02:52

    Don't recycle it yet, because I'm in the middle of using it.
    Don't recycle it yet, because I'm in the middle of using it.

  • 02:55

    If you don't want to use View Property Animator, another way
    If you don't want to use View Property Animator, another way

  • 02:57

    to get the same capability, again, in the Jelly Bean
    to get the same capability, again, in the Jelly Bean

  • 02:59

    release is to use this new property
    release is to use this new property

  • 03:01

    called Transient State.
    called Transient State.

  • 03:03

    So you can call this method, saying, set
    So you can call this method, saying, set

  • 03:04

    has Transient State.
    has Transient State.

  • 03:05

    And that tells us you're in the middle of using it, and we
    And that tells us you're in the middle of using it, and we

  • 03:08

    won't recycle it until you tell us to.
    won't recycle it until you tell us to.

  • 03:11

    So, again, we delete some things.
    So, again, we delete some things.

  • 03:12

    We fling the list.
    We fling the list.

  • 03:13

    And we have no disruptive experience on the screen,
    And we have no disruptive experience on the screen,

  • 03:16

    because we know that you are still using those views that
    because we know that you are still using those views that

  • 03:19

    you are fading out.
    you are fading out.

  • 03:20

    So interesting demo, more interesting code.
    So interesting demo, more interesting code.

  • 03:23

    Let's take a look at that.
    Let's take a look at that.

  • 03:24

    So we have this class called ListView Animations.
    So we have this class called ListView Animations.

  • 03:27

    We create the check boxes and the list view and the adapter,
    We create the check boxes and the list view and the adapter,

  • 03:30

    all that stuff.
    all that stuff.

  • 03:31

    The interesting part is down here.
    The interesting part is down here.

  • 03:32

    What happens when we click on an item?
    What happens when we click on an item?

  • 03:34

    So when we click on it, we're immediately
    So when we click on it, we're immediately

  • 03:36

    going to run this animation.
    going to run this animation.

  • 03:37

    But how we run it depends on which of those check boxes we
    But how we run it depends on which of those check boxes we

  • 03:40

    check at the top for using View Property Animator, or
    check at the top for using View Property Animator, or

  • 03:43

    Transient State, or none of those.
    Transient State, or none of those.

  • 03:45

    So in the none of those case, basically, we wind up in this
    So in the none of those case, basically, we wind up in this

  • 03:49

    code down here.
    code down here.

  • 03:49

    We say, OK, I'm going to create an Object Animator.
    We say, OK, I'm going to create an Object Animator.

  • 03:52

    And we're going to animate.
    And we're going to animate.

  • 03:54

    In a previous episode, we saw some basics on how to use
    In a previous episode, we saw some basics on how to use

  • 03:57

    Object Animator.
    Object Animator.

  • 03:57

    We use a target object.
    We use a target object.

  • 03:59

    We use a target property.
    We use a target property.

  • 04:00

    We're going to animate the alpha property on that view.
    We're going to animate the alpha property on that view.

  • 04:02

    And we're going to animate it to zero from whatever value it
    And we're going to animate it to zero from whatever value it

  • 04:05

    has right now, which happens to be one, fully opaque.
    has right now, which happens to be one, fully opaque.

  • 04:07

    It usually is.
    It usually is.

  • 04:09

    So we create the animation.
    So we create the animation.

  • 04:10

    We set a duration of 1,000.
    We set a duration of 1,000.

  • 04:12

    Way too long for this operation, but for the
    Way too long for this operation, but for the

  • 04:14

    purposes of this demo, I wanted you to actually be able
    purposes of this demo, I wanted you to actually be able

  • 04:16

    to see the animation and the artifact as it was happening.
    to see the animation and the artifact as it was happening.

  • 04:20

    The transient state is not checked.
    The transient state is not checked.

  • 04:23

    So we're going to skip this code here, which is the
    So we're going to skip this code here, which is the

  • 04:25

    interesting part, which we'll get to later.
    interesting part, which we'll get to later.

  • 04:27

    We're going to add a listener so that we can actually
    We're going to add a listener so that we can actually

  • 04:30

    restore these things later and then remove it
    restore these things later and then remove it

  • 04:33

    from the data set.
    from the data set.

  • 04:34

    So on animation end, when this nice, little fade animation
    So on animation end, when this nice, little fade animation

  • 04:37

    out to zero ends, we're going to go in here and actually
    out to zero ends, we're going to go in here and actually

  • 04:40

    remove it from the list of cheeses.
    remove it from the list of cheeses.

  • 04:42

    Notify the data set change, restore the alpha property,
    Notify the data set change, restore the alpha property,

  • 04:45

    which is always important to do with fading animations, and
    which is always important to do with fading animations, and

  • 04:48

    then we're done.
    then we're done.

  • 04:49

    And then we start the animation.
    And then we start the animation.

  • 04:50

    So life is good unless that view got scrolled off the
    So life is good unless that view got scrolled off the

  • 04:53

    screen after the animation started, in which case, the
    screen after the animation started, in which case, the

  • 04:56

    view has been reassigned to different content when you get
    view has been reassigned to different content when you get

  • 04:58

    that disruptive experience that we saw earlier.
    that disruptive experience that we saw earlier.

  • 05:02

    So the two workarounds for it are to use a
    So the two workarounds for it are to use a

  • 05:04

    View Property Animator.
    View Property Animator.

  • 05:05

    So if you checked that box at the top of this demo, then
    So if you checked that box at the top of this demo, then

  • 05:08

    instead of using Object Animator to animate that view,
    instead of using Object Animator to animate that view,

  • 05:11

    we're going to use View Property Animator.
    we're going to use View Property Animator.

  • 05:12

    And we do that by calling code much like this.
    And we do that by calling code much like this.

  • 05:16

    We say view.animate.
    We say view.animate.

  • 05:17

    That returns the instance of the View Property Animator for
    That returns the instance of the View Property Animator for

  • 05:20

    that specific view.
    that specific view.

  • 05:21

    Set duration, again, we want an unusually long duration for
    Set duration, again, we want an unusually long duration for

  • 05:25

    this animation just for the purposes of this demo.
    this animation just for the purposes of this demo.

  • 05:28

    And then we're going to animate alpha to a value of
    And then we're going to animate alpha to a value of

  • 05:31

    zero from its current value, which happens to be one.
    zero from its current value, which happens to be one.

  • 05:34

    And when this animation finishes, it's going to have
    And when this animation finishes, it's going to have

  • 05:37

    an end action, which does something remarkably similar
    an end action, which does something remarkably similar

  • 05:39

    to what we saw before.
    to what we saw before.

  • 05:41

    It's going to remove the item from the cheese list, notify
    It's going to remove the item from the cheese list, notify

  • 05:43

    data set changed, and restore the alpha property.
    data set changed, and restore the alpha property.

  • 05:46

    And we're done.
    And we're done.

  • 05:46

    No special magic to work around this.
    No special magic to work around this.

  • 05:49

    Instead, internally, we're doing the right thing to tell
    Instead, internally, we're doing the right thing to tell

  • 05:52

    the ListView, don't recycle this view yet, because I'm in
    the ListView, don't recycle this view yet, because I'm in

  • 05:55

    the middle of using it.
    the middle of using it.

  • 05:55

    And then when that animation is done, it unsets that flag,
    And then when that animation is done, it unsets that flag,

  • 05:59

    and that view can be recycled appropriately.
    and that view can be recycled appropriately.

  • 06:02

    The alternate way to do it, if you're not using View Property
    The alternate way to do it, if you're not using View Property

  • 06:05

    Animator, you can have access to this exact same
    Animator, you can have access to this exact same

  • 06:08

    functionality by calling set has transient state.
    functionality by calling set has transient state.

  • 06:11

    So if you checked that other box at the top of the demo
    So if you checked that other box at the top of the demo

  • 06:14

    that said Transient State, then we will say, set has
    that said Transient State, then we will say, set has

  • 06:19

    transient state.
    transient state.

  • 06:20

    So when you click on the item, we're automatically going to
    So when you click on the item, we're automatically going to

  • 06:22

    flag that item in the ListView as having transition state,
    flag that item in the ListView as having transition state,

  • 06:25

    which means the ListView will not recycle that item until
    which means the ListView will not recycle that item until

  • 06:29

    you tell it it's OK.
    you tell it it's OK.

  • 06:30

    So we're going to set has transient state.
    So we're going to set has transient state.

  • 06:32

    We're going to run the same exact animation as before.
    We're going to run the same exact animation as before.

  • 06:34

    But OnAnimation end is going to run this little additional
    But OnAnimation end is going to run this little additional

  • 06:38

    bit of code, which restores the transient state to its
    bit of code, which restores the transient state to its

  • 06:40

    default state of false.
    default state of false.

  • 06:42

    So you click on the item.
    So you click on the item.

  • 06:44

    We start running the Object Animator.
    We start running the Object Animator.

  • 06:46

    It's fading out to View.
    It's fading out to View.

  • 06:46

    It's all happy.
    It's all happy.

  • 06:47

    You've set transient state on it.
    You've set transient state on it.

  • 06:48

    We fling the list.
    We fling the list.

  • 06:50

    That view still exists.
    That view still exists.

  • 06:51

    Even though it's now off-screen and you can't see
    Even though it's now off-screen and you can't see

  • 06:53

    it anymore, it's not being used for other content, which
    it anymore, it's not being used for other content, which

  • 06:56

    is really the key to making this animation
    is really the key to making this animation

  • 06:58

    actually work correctly.
    actually work correctly.

  • 07:00

    So ListView Animations, if you want to do them right, be
    So ListView Animations, if you want to do them right, be

  • 07:04

    aware of that transient state.
    aware of that transient state.

  • 07:05

    And don't try to animate stuff that's going to be recycled
    And don't try to animate stuff that's going to be recycled

  • 07:07

    with other content.
    with other content.

  • 07:09

    Thanks.
    Thanks.

All noun
android
/ˈanˌdroid/

word

Robot that looks or seems human

DevBytes: ListView Animations

59,284 views

Video Language:

  • English

Caption Language:

  • English (en)

Accent:

  • English

Speech Time:

97%
  • 7:06 / 7:16

Speech Rate:

  • 207 wpm - Fast

Category:

  • Science & Technology

Intro:

CHET HAASE: Hi, I'm Chet Haase from the Android team at
Google, where I work on graphics and animations.. And today I wanted to talk a little bit more about ListView
animations, which I talked about in a previous episode.
This is another episode in a series that I like to call,
You're Doing It Wrong.. So this is another item related to the last one where
there is a need to understand how views are. actually used by ListView.. ListView recycles views, which means that when those views
are not on the screen, it may use that view to hold
different content.. And you need to be aware of that, especially when you're
running animations, so that you're not. animating the wrong thing.. So to demonstrate that, let's take a look at a little demo.
So we have this demo that I call ListView Animations.
And there's a long list, and quite a. boring one, of cheeses.. Now, as I click on these items, they're going to.

Video Vocabulary

/ˈprēvēəs/

adjective

existing or occurring before in time or order.

/iˈspeSHəlē/

adverb

More than usual; extremely.

/ˈdemənˌstrāt/

verb

To protest about something often as a group.

/ˈdif(ə)rənt/

adjective

not same as another or each other.

/əˈnəT͟Hər/

adjective determiner pronoun

One more, but not this. One more added. One more (thing).

/ˈak(t)SH(o͞o)əlē/

adverb

as truth or facts.

/ˌôdəˈmadiklē/

adverb

by itself with little or no direct human control.

/ˈepəˌsōd/

noun

event or group of events.

/ˌəndərˈstand/

verb

To know the meaning of language, what someone says.

/ˈrəniNG/

adjective noun verb

flowing naturally or supplied to building through pipes and taps. action or movement of runner. To cause an animal to move in a certain direction.

/rēˈsīk(ə)l/

verb

To use old ideas or concepts again.

verb

To give life, energy, or motion to something.

/rəˈmo͞ovd/

adjective verb

separated in relationship by number of steps of descent. To get rid of something completely.

/ˈɡrafiks/

noun other

products of graphic arts. Images on a computer screen or TV.