自定义 Spinner 项目中的可点击按钮会阻止点击项目本身
Clickable button in custom Spinner item blocks clicking of item itself
我有自定义 ArrayAdapter
和 Spinner
的自定义项目布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_toStartOf="@id/delete"
android:drawablePadding="8dp"
android:gravity="center_vertical|start"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:ellipsize="none"
android:textAppearance="@style/TextAppearance.AppCompat.Menu" />
<ImageButton
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:background="?attr/transparentRoundRipple"
android:src="@drawable/ic_delete_grey600_24px" />
</RelativeLayout>
在这个布局中有一个按钮可以删除这个项目
@Override
public View getDropDownView(int position, View view, ViewGroup parent)
{
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.spinner_user_agent_item, parent, false);
}
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(getItem(position));
ImageButton deleteButton = view.findViewById(R.id.delete);
deleteButton.setOnClickListener((View v) -> removeAgent(getItem(position)));
return view;
}
但问题是此按钮的侦听器会阻止元素本身的单击,我无法将其从列表中 select。是否可以更改行为并使其能够单击按钮(用于删除)和元素本身(对于 selection)?
尝试将 android:clickable="true"
添加到 RelativeLayout
以及 android:focusable="true"
和 android:focusableInTouchMode="true"
解决方案很简单:使用 ImageView
而不是 ImageButton
。
我有自定义 ArrayAdapter
和 Spinner
的自定义项目布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_toStartOf="@id/delete"
android:drawablePadding="8dp"
android:gravity="center_vertical|start"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:ellipsize="none"
android:textAppearance="@style/TextAppearance.AppCompat.Menu" />
<ImageButton
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:background="?attr/transparentRoundRipple"
android:src="@drawable/ic_delete_grey600_24px" />
</RelativeLayout>
在这个布局中有一个按钮可以删除这个项目
@Override
public View getDropDownView(int position, View view, ViewGroup parent)
{
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.spinner_user_agent_item, parent, false);
}
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(getItem(position));
ImageButton deleteButton = view.findViewById(R.id.delete);
deleteButton.setOnClickListener((View v) -> removeAgent(getItem(position)));
return view;
}
但问题是此按钮的侦听器会阻止元素本身的单击,我无法将其从列表中 select。是否可以更改行为并使其能够单击按钮(用于删除)和元素本身(对于 selection)?
尝试将 android:clickable="true"
添加到 RelativeLayout
以及 android:focusable="true"
和 android:focusableInTouchMode="true"
解决方案很简单:使用 ImageView
而不是 ImageButton
。