首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >平稳地从芯片组中删除android芯片

平稳地从芯片组中删除android芯片
EN

Stack Overflow用户
提问于 2019-10-15 22:27:48
回答 1查看 9.1K关注 0票数 10

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

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

布局:

代码语言:javascript
复制
       <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> 

芯片删除:

代码语言:javascript
复制
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);
}

芯片布局:

代码语言:javascript
复制
<?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" />

我想要一个平滑动画,当“中间芯片”被删除时,芯片平滑地向左移动。我试过几件事,但没有运气。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-09 08:22:34

如果你是说这样的话

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

代码语言:javascript
复制
<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添加了芯片。

代码语言:javascript
复制
val newChip = Chip(this, null, R.attr.chipStyle)
newChip.text = "text"
newChip.isClickable = true
newChip.isFocusable = true
newChip.setOnClickListener(chipClickListener)
chip_group.addView(newChip)

每个chip.inside上都有一个chip.inside,我启动一个淡入动画,在onAnimationEnd中,在chip_group上做一个removeView

代码语言:javascript
复制
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)
    }
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58403377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档