Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Slide ListView with setX, setPadding, or tween animation?

Without going into too much detail, I want to be able to 'slide' elements in a ListView similar to the 'slide to archive' feature in GMail. I'm fine with the onTouchListener and all that, my question is regarding the slide animation.

The first two things that come to mind are..

view.setPadding(slideOffset, 0, 0, 0);

and..

view.setX(slideOffset);

The former is very buttery, even on the emulator.
The latter is a bit janky on my Galaxy Nexus.

My questions:
* Regardless of what I've tried, what's the correct way to do this?
Why is setX less smooth than setPadding?
Does one approach conform to Android best practices more than the other?
Are tweened translation animations an option? If so, can you provide a brief example to point me in the right direction please?

Edit:
To be clear, I am attaching an image of the effect I am trying to emulate.

enter image description here

like image 667
Sean Connolly Avatar asked Oct 22 '22 18:10

Sean Connolly


1 Answers

I'm pretty sure the setX() is slower because it affects its parents. When changing the X of a view, it calls the onLayout/onMeasure of the parent every time you update the value. That's because the X value of the child may cause other items on the parent to move, therefor the parent needs to redraw itself.

You can test this easily by extending the ViewGroup and writing to the log on those methods. Then, you can use both approaches, padding vs. setX, and see what happens.

Are you trying to animate the item? Or do you want the user to move it like on Gmail? You can use the ObjectAnimator to handle the "X" value of your item. Combined with a "hardware layer" for your item, it will create a smoother experience. You can find more details about how to do that here: http://developer.android.com/guide/topics/graphics/hardware-accel.html

like image 161
Udinic Avatar answered Oct 24 '22 12:10

Udinic