Android AppCompatSpinner不会关闭,不会调用onItemSelectedListener

时间:2018-07-15 06:11:42

标签: android android-spinner

我已经为AppCompatSpinner创建了自定义适配器。自定义适配器如下:

class MyAdapter(context: Context, var itemViewRes: Int, var dataSource: List<Product>) : ArrayAdapter<Product>(context, itemViewRes, dataSource) {

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
    return getCustomView(position, convertView, parent);
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    return getCustomView(position, convertView, parent)
}


private fun getCustomView(position: Int, convertView: View?, parent: ViewGroup?): View {
    lateinit var row: View

    parent?.let {
        convertView?.let {
            row = convertView
        } ?: run {
            with(Extensions) {
                row = parent.inflate(itemViewRes)
            }
        }
    }

    with(row) {
        tv_title.text = dataSource[position].displayColor
        var colorHex = dataSource[position].displayColor

        if ("#ffffff".equals(colorHex, ignoreCase = false)) {
            colorHex = "#ededed" //use off-white color as its easier to see
        }

        val backgroundGradient = ib_color.background as? GradientDrawable
        backgroundGradient?.setColor(Color.parseColor(colorHex))

        if (dataSource[position].selectedPosition) {
            (ib_color_container.background as? GradientDrawable)?.setColor(ContextCompat.getColor(context, R.color.black))
        } else
            (ib_color_container.background as? GradientDrawable)?.setColor(Color.parseColor(colorHex))

    }
    return row
}

}

这是项目下拉视图xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.Guideline
    android:id="@+id/guideline_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="20dp" />

<LinearLayout
    android:id="@+id/ib_color_container"
    android:layout_width="20dp"
    android:layout_height="20dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="@id/guideline_start"
    android:background="@drawable/round_btn">

<ImageButton
    android:id="@+id/ib_color"
    android:layout_width="18dp"
    android:layout_height="18dp"
    android:clickable="false"
    android:layout_gravity="center"
   android:background="@drawable/round_btn" />

</LinearLayout>

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Regular-Extd.otf"
    android:textSize="11sp"
    android:layout_marginLeft="15dp"
    android:textAllCaps="true"
    android:text="pink"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@id/ib_color_container"
    />

以下是片段布局文件中如何定义微调器:

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"/>

最后是片段类调用的微调器设置:

private fun initColorsDropDown(productModel: ProductModel) {
    var productColors = getAllColorProducts(productModel)

    adapterColors = MyAdapter(requireContext(), R.layout.product_details_color_item_row, productColors)
    adapterColors?.setDropDownViewResource(R.layout.product_details_color_item_row)
    spinner_color.adapter = adapterColors
    spinner_color.onItemSelectedListener = this
}

当我在android api 27中运行程序时,我能够向微调器加载数据。问题是当我单击一个项目时,听众不会被解雇。但更奇怪的是,微调器没有关闭。无论我单击哪里,它都在那里。但是,如果我在微调框下拉列表之外单击,它的确关闭。我究竟做错了什么 ?我还尝试了扩展SpinnerAdapter而不是arrayAdapter,并且不使用constraintLayout但存在相同的问题。

1 个答案:

答案 0 :(得分:0)

12小时后,我终于弄清楚了。您的所有行项目都必须设置android:focusable="false"。我的行项目现在看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.Guideline
    android:id="@+id/guideline_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"

    app:layout_constraintGuide_begin="20dp" />

<LinearLayout
    android:id="@+id/ib_color_container"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="@id/guideline_start"
    android:background="@drawable/round_btn">

<ImageView
    android:id="@+id/ib_color"
    android:layout_width="18dp"
    android:layout_height="18dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_gravity="center"
   android:background="@drawable/round_btn" />

</LinearLayout>

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Regular-Extd.otf"
    android:textSize="11sp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_marginLeft="15dp"
    android:textAllCaps="true"
    android:text="pink"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@id/ib_color_container"
    />