android - 带有可拖动芯片的芯片组

标签 android android-chips

在我的 XML 中,我只是声明了一个 ChipGroup如下:

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chipGroup" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
然后添加每个 Chip动态(其中 CustomChipFilterStyle 将它们设置为 Chip 的“过滤器”类型):
ChipGroup chipGroup = findViewById(R.id.chipGroup);
for (String name : names) {
    Chip chip = new Chip(this, null, R.attr.CustomChipFilterStyle);
    chip.setText(name);
    chipGroup.addView(chip);
}
guidance (请参阅“可移动”下的视频剪辑)它表明“输入芯片可以重新排序或移动到其他字段”:
enter image description here
但是我看不到有关如何完成此操作的任何指导,也找不到任何示例。它是完全定制的东西(通过 View.OnDragListenerchip.setOnDragListener() ),还是作为 Chip 的一部分的实用方法?框架?我真正需要做的就是重新排序Chip s 在同一个 ChipGroup .我确实从 chip.setOnDragListener() 开始但很快意识到我没有足够的知识来创建必要的动画来轻推和重新排序其他 Chip s 为 Chip本身正在被拖动(并区分点击 - 过滤 - 和拖动)......我希望可能有一些开箱即用的方式来做到这一点 ChipGroup可能在上述指导中提到过。

最佳答案

But I can't see any guidance about how [chip reordering within a ChipGroup] is done, or find any examples out there.


令人惊讶的是,似乎没有一种“开箱即用”的方式来重新排序 ChipGroup 中的芯片——至少我没有找到。

All I really need to be able to do is to reorder Chips within the same ChipGroup.


I did start with chip.setOnDragListener() but soon realised I didn't have sufficient knowledge about how to create the necessary animations to nudge and re-order other Chips as the Chip itself is being dragged


以下内容并没有真正完全回答您的问题,因为答案涉及 RecyclerView 而不是 ChipGroup,但效果是一样的。此解决方案基于 ItemTouchHelper Demo 保罗·伯克。我已经将 Java 转换为 Kotlin 并对代码进行了一些修改。我在 ChipReorder 上发布了一个演示 repo我用于 RecyclerView 的布局管理器是 FlexboxLayoutManager .
演示应用程序依赖于 ItemTouchHelper这是一个实用程序类,它添加滑动以关闭和拖放支持到 RecyclerView。如果您查看 ItemTouchHelper 的实际代码,您将了解显示在屏幕上的动画的底层复杂性,以便进行简单的拖动。
这是使用演示应用程序拖动芯片的快速视频。
enter image description here
我相信您可能需要从 ChipGroup 获得的任何功能都可以通过 RecyclerView 或其适配器来实现。
更新:我在演示存储库中添加了一个名为“chipgroupreorder”的模块,该模块通过动画对 ChipGroup 中的芯片进行重新排序。尽管这看起来与 RecyclerView 解决方案非常相似,但它使用的是 ChipGroup 而不是 RecyclerView。
该演示使用 View.OnDragListener并依赖 android:animateLayoutChanges="true"这是为动画的 ChipGroup 设置的。
选择要转换的 View 是基本的,可以改进。进一步测试可能会出现其他问题。
enter image description here

关于android - 带有可拖动芯片的芯片组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71193266/

相关文章:

android - 使用安卓 Material 芯片

android - 为 android 2.2 编码的 facebook sdk 无法在 android 4.0.4 上运行,显示错误 android.os.Networkonmainthread

java - 字符串连接和附加在 foreach 中不起作用

java - 动态添加芯片到芯片组

android - android支持库中的芯片组件?

android - 芯片组多排芯片

android - 顶点属性未传递给着色器

android - android中IME UI的透明背景

Android fragment 从布局资源中膨胀,在顶部显示一个额外的 strip

Android 芯片 View 不使用 android :textAllCaps ="true" 大写字母