This repository was archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
Expand file tree
/
Copy pathAdapterView.kt
More file actions
113 lines (105 loc) · 3.8 KB
/
AdapterView.kt
File metadata and controls
113 lines (105 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.core.widget
import android.view.View
import android.widget.AdapterView
/**
* Sets click listener with automatic item casting
* (ClassCastException will be thrown if adapter's item type does not match)
*/
inline fun <T> AdapterView<*>.onItemClick(crossinline onItemClick: (item: T) -> Unit) {
setOnItemClickListener { parent, _, position, _ ->
@Suppress("UNCHECKED_CAST")
onItemClick(parent.getItemAtPosition(position) as T)
}
}
/**
* Sets long click listener with automatic item casting
* (ClassCastException will be thrown if adapter's item type does not match)
*/
inline fun <T> AdapterView<*>.onItemLongClick(crossinline onItemLongClick: (item: T) -> Boolean) {
setOnItemLongClickListener { parent, _, position, _ ->
@Suppress("UNCHECKED_CAST")
onItemLongClick(parent.getItemAtPosition(position) as T)
}
}
/**
* Simple use case (empty `onNothingSelected` default provided):
* ```kotlin
* spinner.onItemSelected { parent, _, position, _ ->
* val item = parent.getItemAtPosition(position)
* ???
* }
* ```
* Use case with `onNothingSelected` handling:
* ```kotlin
* spinner.onItemSelected(
* onNothingSelected = { _: AdapterView<*> -> ??? },
* onItemSelected = { parent, _, position, _ ->
* val item = parent.getItemAtPosition(position)
* ???
* })
* ```
* @see android.widget.AdapterView.OnItemSelectedListener
*/
inline fun AdapterView<*>.onItemSelected(
crossinline onNothingSelected: (parent: AdapterView<*>) -> Unit = {},
crossinline onItemSelected: (
parent: AdapterView<*>,
view: View?,
position: Int,
id: Long
) -> Unit
) {
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>) = onNothingSelected(parent)
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
onItemSelected(parent, view, position, id)
}
}
}
/**
* Sets selection listener with automatic item casting
* (ClassCastException will be thrown if adapter's item type does not match)
*
* Simple use case (empty `onNothingSelected` default provided):
* ```kotlin
* spinner.onItemSelected { item: T -> ??? }
* ```
* Use case with `onNothingSelected` handling:
* ```kotlin
*
* spinner.onItemSelected(
* onNothingSelected = { ??? },
* onItemSelected = { item: String -> ??? }
* )
* ```
* @param onNothingSelected optional action, default `{}`
* @param onItemSelected action with casted item passed
* @see android.widget.AdapterView.OnItemSelectedListener
*/
inline fun <T> AdapterView<*>.onItemSelected(
crossinline onNothingSelected: () -> Unit = {},
crossinline onItemSelected: (item: T) -> Unit
) {
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>) = onNothingSelected()
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
@Suppress("UNCHECKED_CAST")
onItemSelected(parent.getItemAtPosition(position) as T)
}
}
}