顺利从芯片组中移除一个安卓芯片

m.i*_*.r. 7 android android-animation android-chips androidx

在我正在开发的应用程序的一个片段中,我让用户创建各种芯片,每个芯片代表一个选项。我能够为芯片创建动画

现在,当用户点击芯片时,我将其从组中删除。我能够将自定义动画与删除相关联(参见 gif),但是当删除“中间筹码”时,右侧的筹码会在动画结束时突然向左移动

在此处输入图片说明

布局:

       <HorizontalScrollView
           android:layout_width="match_parent"
           android:layout_height="@dimen/horizontal_scroll_height"
           android:scrollbars="none">

           <com.google.android.material.chip.ChipGroup
               android:id="@+id/rouletteChipList"
               style="@style/Widget.MaterialComponents.ChipGroup"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:paddingStart="@dimen/chip_horizontal_margin"
               android:paddingEnd="@dimen/chip_horizontal_margin"
               app:chipSpacing="@dimen/chip_spacing"
               app:singleLine="true">

           </com.google.android.material.chip.ChipGroup>
       </HorizontalScrollView> 
Run Code Online (Sandbox Code Playgroud)

芯片删除:

private void removeChip(final Chip chip) {
        @SuppressWarnings("ConstantConditions") final ChipGroup optionsList = getView().findViewById(R.id.ChipList);
        // Remove the chip with an animation
        if (chip == null) return;
        final Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.chip_exit_anim);
        chip.startAnimation(animation);
        chip.postDelayed(new Runnable() {
            @Override
            public void run() {
                optionsList.removeView(chip);
            }
        }, 400);
}
Run Code Online (Sandbox Code Playgroud)

芯片布局:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip 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="wrap_content"
    android:text="@string/placeholder"
    android:textAlignment="center"
    app:chipBackgroundColor="@color/grayTranslucent"
    app:rippleColor="?colorAccent" />
Run Code Online (Sandbox Code Playgroud)

我想要一个流畅的动画,当删除“中间筹码”时筹码会平稳地向左移动。我尝试了几件事,但没有运气。

Moj*_*adi 12

如果你的意思是这样的

动画移除筹码

我已经将它添加android:animateLayoutChanges="true"到HSV 中并添加了chip_group,请参阅下面的代码

<HorizontalScrollView
        android:scrollbars="none"
        .
        >
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chip_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true">

        </com.google.android.material.chip.ChipGroup>

    </HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我已经动态添加了芯片chip_group

val newChip = Chip(this, null, R.attr.chipStyle)
newChip.text = "text"
newChip.isClickable = true
newChip.isFocusable = true
newChip.setOnClickListener(chipClickListener)
chip_group.addView(newChip)
Run Code Online (Sandbox Code Playgroud)

并有onClickListener每个chip.inside它,我开始淡出动画和在onAnimationEndremoveViewchip_group

private val chipClickListener = View.OnClickListener {

        val anim = AlphaAnimation(1f,0f)
        anim.duration = 250
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {}

            override fun onAnimationEnd(animation: Animation?) {
                chip_group.removeView(it)
            }

            override fun onAnimationStart(animation: Animation?) {}
        })

        it.startAnimation(anim)
    }
Run Code Online (Sandbox Code Playgroud)