Android switchcompat plays animation on toggle() or setchecked()

I added switchcompat. Com to Android's drawrnavigation

First, I set the actionlayout of the project to switchlayout.xml:

<android.support.v7.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right|center_vertical"
app:buttonTint="@color/colorPrimary"
app:switchPadding="16dp" />

Now, I'm trying to change the buttons checkstate in the code, using switchcopmatobj. Toggle(), and switchcompatobj. Setchecked (! Switchcompatobj. Ischecked())

However, it will only change the color of the switch and will not play the animation of the switch moving from one side to the other. How do I play this animation from the code?

resolvent:

I have the same problem. It seems that switchcompat is corrupted at this point. The setchecked() call should update the animator of the thumb paintable object, but onanimationend will never be called

Meanwhile, after trying to disguise the touch event sent to switchcompat as many things, I found an ugly solution: instead of calling setchecked (), I deleted the item from the menu and added it again:

// Update toggle state
mDrawerView.getMenu().removeItem(R.id.drawer_menu_availability);
final SwitchCompat toggle = (SwitchCompat) LayoutInflater.from(this).inflate(R.layout.drawer_menu_item_availability, null);
toggle.setChecked(mAvailable);
toggle.setOnCheckedchangelistener((buttonView, isChecked) -> toggleAvailability());
final MenuItem added = mDrawerView.getMenu().add(R.id.drawer_menu_group_actions, R.id.drawer_menu_availability, Menu.FIRST, text);
added.setActionView(toggle);

However, this forces you to:

>Have the ID of the menu group where the menu item is located > order the menu item using Android: orderincategory to return it to the same location when you add it back

I hope this will help

Edit: this will not play the animation, but at least the thumb is in the right position

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>