Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Switch state without animation

In my Android project, I have a ListView with rows containing SwitchCompat items (AppCompat for Switch widget).

My problem occurs when I scroll into the list and getView(...) method of MyAdapter is invoked with a recycled view. I redefine the correct Switch state but the animation is visible.

There is a solution to prevent the animation in this case?

enter image description here

like image 328
alex Avatar asked Nov 26 '14 00:11

alex


People also ask

How to change the state of an animator?

To chage it to a default state you may link each state to “Exit”, then the state is infinitely called again. If it want to change the “Stay” animation, a parameter of Animator need to be initialized. You change the parameter to “Stay” value after switching a state. If a project is simple action game, the above code have nothing problem.

How to change the “stay” animation in animator?

If it want to change the “Stay” animation, a parameter of Animator need to be initialized. You change the parameter to “Stay” value after switching a state.

How to use animator “any state” in Unity?

Environment: Unity 2018.4.6f How to use Animator “Any State”. The way for simple action game. Take measure for continuous inputs as combat game. A state linked to “Any State” can transition from all states. But if it’s just linked by “make transition”, same animation is called many times. It need uncheck “ Can Transition To Self “.

Can transition to self animation be called many times?

But if it’s just linked by “make transition”, same animation is called many times. It need uncheck “ Can Transition To Self “. This parameter is on a cursor of transition. The state isn’t called many times but at the end of animation a model just stand.


2 Answers

Call jumpDrawablesToCurrentState() to skip the animation

switchCompat.setChecked(true); switchCompat.jumpDrawablesToCurrentState(); 
like image 93
Lin Avatar answered Oct 09 '22 04:10

Lin


I finally found a solution but seems not really clean:

ViewGroup viewGroup = (ViewGroup) view; // the recycled view viewGroup.removeView(switch); switch.setChecked(states[index]); viewGroup.addView(switch); 

If a better solution exists, please share it.

like image 24
alex Avatar answered Oct 09 '22 04:10

alex