从芯片组中顺利移除android芯片
在我正在开发的应用程序的一个片段中,我让用户创建各种芯片,每个芯片代表一个选项。我能够制作芯片的动画 现在,当用户点击芯片时,我将其从组中移除。我可以将自定义动画与删除关联(请参见gif),但当删除“中间芯片”时,动画结束时,右侧的芯片突然向左移动 布局:从芯片组中顺利移除android芯片,android,android-animation,androidx,android-chips,Android,Android Animation,Androidx,Android Chips,在我正在开发的应用程序的一个片段中,我让用户创建各种芯片,每个芯片代表一个选项。我能够制作芯片的动画 现在,当用户点击芯片时,我将其从组中移除。我可以将自定义动画与删除关联(请参见gif),但当删除“中间芯片”时,动画结束时,右侧的芯片突然向左移动 布局: <HorizontalScrollView android:layout_width="match_parent" android:layout_height="@dimen/
<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" />
芯片布局:
<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" />
我想要一个平滑的动画,当删除“中间的芯片”时,芯片会平滑地向左移动。我试过几次,但没有成功。如果你是说这样的话 i、 我在HSV中添加了它,并在
chip\u 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>
每个芯片上都有一个onClickListener
。在它里面我开始一个淡入淡出的动画,然后在onAnimationEnd
中在芯片组上做一个removeView
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)
}
使用RecyclerView而不是水平滚动视图您能详细说明一下吗?我从来没用过任何回收工具,Sadlyn,没这么简单。在材料组件库的目录中,有一个包含芯片和回收视图的。在remove上没有动画,但它在recyclerview上是一个简单的动画。我会给它一个镜头,谢谢。我会用任何进展更新问题。@m.I.n.a.r:好的,我在等你:DIt工作完美!非常感谢,我迷失在复杂的解决方法中,这只是一句话:D