Android多选用户界面
我尽我所能尝试搜索,但我甚至不知道正确的关键字。我想要一个效果类似于下面的屏幕截图时,选择多个东西Android多选用户界面,android,user-interface,Android,User Interface,我尽我所能尝试搜索,但我甚至不知道正确的关键字。我想要一个效果类似于下面的屏幕截图时,选择多个东西 这样做的目的是很容易看到谁被选中,只需单击“x”即可从选择中删除。我不知道这样一个组件是否已经存在,或者我是否需要手动创建它,如果是,我不知道如何创建。我得到的最接近的是一个多选列表视图,但理想情况下这不是我想要的。谷歌将这种类型的UI提供称为芯片 您最可能需要的芯片容器是FlowLayout 问题是谷歌没有现成的组件来实现这些功能 以下是我制作芯片的方法: /res/drawable/chi
这样做的目的是很容易看到谁被选中,只需单击“x”即可从选择中删除。我不知道这样一个组件是否已经存在,或者我是否需要手动创建它,如果是,我不知道如何创建。我得到的最接近的是一个多选列表视图,但理想情况下这不是我想要的。谷歌将这种类型的UI提供称为芯片 您最可能需要的芯片容器是
FlowLayout
问题是谷歌没有现成的组件来实现这些功能
以下是我制作芯片的方法:
/res/drawable/chip_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/translucent_gray"/>
<corners android:radius="16dp"/>
</shape>
/res/layout/chip.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@drawable/chip_background">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:maxLines="1"
android:ellipsize="end"
android:textSize="13sp"
android:textColor="@android:color/white"
tools:text="Sample Text"/>
<ImageButton
android:id="@+id/removeButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@null"
android:alpha=".8"
android:scaleType="fitCenter"
android:padding="7dp"
android:src="@drawable/ic_cancel_white_24dp"/>
</LinearLayout>
移除按钮上的填充和缩放类型将24dp图标向下收缩一点,以便很好地适合芯片椭圆形
对于
FlowLayout
,您可以在GitHub中搜索“Android FlowLayout”并找到要使用的库。您可以使用my ForRecyclerView来尝试解决方案。有了它,您只需创建一个带有RemoveListener的适配器(就像您为任何其他类型的LayoutManager
)和项目所做的那样)
ChipsLayoutManager chipsLayoutManager = ChipsLayoutManager.newBuilder()
.build();
recyclerView.setLayoutManager(chipsLayoutManager);
//your adapter
adapter = new RecyclerViewAdapter(items, onRemoveListener);
在侦听器中,只需从项目列表中删除已删除的项目并更新适配器
private OnRemoveListener onRemoveListener = new OnRemoveListener() {
@Override
public void onItemRemoved(int position) {
items.remove(position);
adapter.notifyItemRemoved(position);
}
};
该项目将随动画一起删除。谢谢,这正是我所需要的,我怀疑在不知道寻找芯片的情况下是否会找到它。