我有自定义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;
}
但是问题是此按钮的侦听器阻止了元素本身的单击,因此我无法从列表中选择它。是否可以更改行为并将其更改为使我可以单击按钮(用于删除)和元素本身(用于选择)?
答案 0 :(得分:0)
还尝试将android:clickable="true"
和RelativeLayout
的{{1}}和android:focusable="true"
添加到android:focusableInTouchMode="true"
答案 1 :(得分:0)
解决方案很简单:使用ImageView
代替ImageButton
。