【问题标题】:Spinner - show hint when adapter is emptySpinner - 当适配器为空时显示提示
【发布时间】:2013-09-05 22:06:08
【问题描述】:

我知道有几个问题涉及如何在第一次选择之前为Spinner 添加“选择一个...”提示。但这不是我的情况。

我需要的是在SpinnerAdapter 为空时显示提示。在这种情况下,默认情况下,点击什么都不会发生(但这不是主要问题),最重要的是,微调器不显示任何文本,所以看起来像这样,显然感觉不对:

知道如何简单地处理这个问题吗?我提出了 2 个可能的解决方案,但我不太喜欢其中任何一个:

  • 如果 SpinnerAdapter 为空,请从布局中隐藏 Spinner,并改为显示与 Spinner 背景相同的 TextView
  • 实现一个自定义SpinnerAdapter,如果内部列表为空,则其getCount()返回1而不是0,同时,使其getView()返回一个TextView,其中包含所需的“Empty” “消息,可能是灰色的。但这需要具体检查所选项目是否不是“空”项目。

【问题讨论】:

    标签: android android-spinner


    【解决方案1】:

    你可以在下面使用这个SpinnerWithHintAdapter

    class SpinnerWithHintAdapter(context: Context, resource: Int = android.R.layout.simple_spinner_dropdown_item) :
        ArrayAdapter<Any>(context, resource) {
    
        override fun isEnabled(position: Int): Boolean {
            return position != 0
        }
    
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return (super.getDropDownView(position, convertView, parent) as TextView).apply {
                if (position == 0) {
                    // Set the hint text color gray
                    setTextColor(Color.GRAY)
                } else {
                    setTextColor(Color.BLACK)
                }
            }
        }
    
        fun attachTo(spinner: Spinner, itemSelectedCallback: ((Any?) -> Unit)? = null) {
            spinner.apply {
                adapter = this@SpinnerWithHintAdapter
                itemSelectedCallback?.let {
                    onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onNothingSelected(parent: AdapterView<*>?) {}
    
                        override fun onItemSelected(
                            parent: AdapterView<*>?,
                            view: View?,
                            position: Int,
                            id: Long
                        ) {
                            val selectedItem = parent?.getItemAtPosition(position)
                            // If user change the default selection
                            // First item is disable and it is used for hint
                            if (position > 0) {
                                it(selectedItem)
                            }
                        }
                    }
                }
            }
        }
    }
    

    如何使用?假设我有一个名为 City

    的数据类
    data class City(
        val id: Int,
        val cityName: String,
        val provinceId: Int
    ) {
        /**
         * By overriding toString function, you will show the dropdown text correctly
         */
        override fun toString(): String {
            return cityName
        }
    }
    

    在活动中,启动适配器,添加提示(第一项),添加主要项目,最后将其附加到您的微调器。

    SpinnerWithHintAdapter(this@MyActivity)
        .apply {
            // add hint
            add("City")
    
            // add your main items
            for (city in cityList) add(city)
    
            // attach this adapter to your spinner
            attachTo(yourSpinner) { selectedItem -> // optional item selected listener
                selectedItem?.apply {
                    if (selectedItem is City) {
                        // do what you want with the selected item
                    }
                }
            }
        }
    

    【讨论】:

    • 无论你的主项是否为空,提示都会显示为第一项。但是,如果您只想在主要项目为空时显示提示,则可以创建 if-else 条件。仅当您的主要项目为空时才使用适配器,如果您的主要项目不为空,则使用标准适配器(ArrayAdapter、SpinnerAdapter 等)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多