【问题标题】:Remove an android Chip from a Chip Group smoothly顺利从芯片组中移除一个安卓芯片
【发布时间】:2019-10-15 22:27:48
【问题描述】:

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

现在,当用户点击芯片时,我将其从组中删除。我能够将自定义动画与删除相关联(参见 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> 

芯片删除:

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);
}

芯片布局:

<?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" />

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

【问题讨论】:

  • 使用 RecyclerView 而不是 Horizo​​ntalScrollView
  • 您能详细说明一下吗?遗憾的是,我从未使用过任何 RecyclerView
  • 没那么简单。在材料组件库的目录中有 an example 带有 Chips 和 RecyclerView。移除时没有动画,但它是 recyclerview 上的简单动画。
  • 我会试一试,谢谢。如果有任何进展,我会更新问题。

标签: android android-animation androidx android-chips


【解决方案1】:

如果你的意思是这样的

我已将其添加到 HSV 中,并在 chip_group 上添加了 android:animateLayoutChanges="true",请参见下面的代码

<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>

在我的代码中,我已将筹码动态添加到此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)

每个芯片上都有一个onClickListener。在它里面我开始一个淡入淡出动画并在onAnimationEnd做一个removeViewchip_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)
    }

【讨论】:

  • @m.i.n.a.r. : 好吧,我在等你 :D
  • 完美运行!非常感谢,我迷失在复杂的解决方法中,这只是一行:D
猜你喜欢
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2019-09-11
  • 2018-12-14
  • 2019-05-23
相关资源
最近更新 更多