收听MultiSelectSpinner关闭事件

时间:2019-02-07 13:59:01

标签: android kotlin android-spinner

我有带MultiSelect选项的Spinner。我想列出微调框以显示关闭事件。因为我需要在此事件上发送服务器请求。

当微调框失去焦点时,我试图倾听,但这并没有帮助我。 如何收听微调器关闭事件或微调器失去焦点?

我也已经尝试过这个答案: https://stackoverflow.com/a/18636385/10638252

但这对我不起作用。

我的自定义微调器类:

class MultiSelectSpinner: Spinner, OnMultiChoiceClickListener{

private var _itemsUsers: Array<String>? = null
private var _itemsProjects:Array<String>? = null
private var _selectionUsers: BooleanArray? = null
private var _selectionProjects: BooleanArray? = null
private var _proxyAdapter: ArrayAdapter<String>
private var _title:String? = null
val usersListOfID = ArrayList<Int>()
private var usersList:List<Users>? = null
private var projectsPOJOList:List<ProjectsPOKO>? = null
val projectsListOfID = ArrayList<Int>()

    /**
     * Returns a list of strings, one for each selected item.
     * @return
     */
val selectedUsers: List<String>
    get() {
        val selection = LinkedList<String>()
        for (i in _itemsUsers!!.indices) {
            if (_selectionUsers!![i]) {
                selection.add(_itemsUsers!![i])
            }
        }
        return selection
    }

val selectedProjects: List<String>
    get() {
        val selection = LinkedList<String>()
        for (i in _itemsProjects!!.indices) {
            if (_selectionProjects!![i]) {
                selection.add(_itemsProjects!![i])
            }
        }
        return selection
    }

/**
 * Constructor for use when instantiating directly.
 * @param context
 */
constructor(context: Context) : super(context) {
    _proxyAdapter = ArrayAdapter(context, android.R.layout.simple_spinner_item)
    super.setAdapter(_proxyAdapter)
}

/**
 * Constructor used by the layout inflater.
 * @param context
 * @param attrs
 */
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    _proxyAdapter = ArrayAdapter(context, android.R.layout.simple_spinner_item)
    super.setAdapter(_proxyAdapter)
}

/**
 * {@inheritDoc}
 */
override fun onClick(dialog: DialogInterface, which: Int, isChecked: Boolean) { //some code}

/**
 * {@inheritDoc}
 */
override fun performClick(): Boolean {
    val builder = AlertDialog.Builder(context)
    val builder2 = AlertDialog.Builder(context)

    builder.setMultiChoiceItems(_itemsUsers, _selectionUsers, this)
    builder2.setMultiChoiceItems(_itemsProjects, _selectionProjects, this)
    if(_itemsUsers!=null){
        builder.show()
    }
    if(_itemsProjects!=null){
        builder2.show()
    }
    return super.performClick()
}

/**
 * MultiSelectSpinner does not support setting an adapter. This will throw an exception.
 * @param adapter
 */
override fun setAdapter(adapter: SpinnerAdapter) {
    throw RuntimeException("setAdapter is not supported by MultiSelectSpinner.")
}

/**
 * Sets the options for this spinner.
 * @param items
 */
fun setUsers(items: List<String>) {
    _itemsUsers = items.toTypedArray()
    _selectionUsers = BooleanArray(_itemsUsers!!.size)

    Arrays.fill(_selectionUsers, false)
}
fun setProjects(items: List<String>) {
    _itemsProjects = items.toTypedArray()
    _selectionProjects = BooleanArray(_itemsProjects!!.size)

    Arrays.fill(_selectionProjects, false)
}

0 个答案:

没有答案