Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Does AdapterView deserve attention? #470

Open
fada21 opened this issue Mar 28, 2018 · 6 comments
Open

Does AdapterView deserve attention? #470

fada21 opened this issue Mar 28, 2018 · 6 comments

Comments

@fada21
Copy link

fada21 commented Mar 28, 2018

I was thinking about adding some AdapterView extentions. Some listeners setting extentions first for those:

  • setOnItemClickListener
  • setOnItemLongClickListener
  • setOnItemSelectedListener

I'm using them for Spinners in my project atm. Should I prepare PR?

@romainguy
Copy link
Collaborator

What would the extensions do?

@fada21
Copy link
Author

fada21 commented Mar 29, 2018

I'll just post method signature here. Later today or tomorrow prepare PR to be more precise and kick off detailed discussion.

@Suppress("UNCHECKED_CAST")
inline fun <ITEM> AdapterView<*>.onItemClick(
    crossinline onItemClick: (item: ITEM) -> Unit
)
@Suppress("UNCHECKED_CAST")
inline fun <ITEM> AdapterView<*>.onItemLongClick(
    crossinline onItemLongClick: (item: ITEM) -> Boolean
)
inline fun AdapterView<*>.onItemSelected(
    crossinline onNothingSelected: () -> Unit = {},
    crossinline onItemSelected: (parent: AdapterView<*>?, view: View?, position: Int, id: Long) -> Unit
)
@Suppress("UNCHECKED_CAST")
inline fun <ITEM> AdapterView<*>.onItemSelected(
    crossinline onNothingSelected: () -> Unit = {},
    crossinline onItemSelected: (item: ITEM) -> Unit
)

Generally some sugar on the AdapterView setting click listeners ceremony.

@sohailehmad
Copy link

sohailehmad commented Apr 2, 2018

setOnItemSelectedListener can be shorten as

inline fun <T> AdapterView<*>.doOnItemSelected(crossinline block: (item:T) -> Unit) {
    onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {
        }

        @Suppress("UNCHECKED_CAST")
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            block(selectedItem as T)
        }
    }
}

Then we can use this extension like this

adapterView.doOnItemSelected<T> {
    //here Type of it will be T
}

@fada21
Copy link
Author

fada21 commented Apr 3, 2018

Work in progres PR is ready #484. I need to finish some tests but most importantly I'm waiting for feedback on implementation.

@fada21
Copy link
Author

fada21 commented Apr 3, 2018

@sohailehmad please note that with default argument for onNothingSelected you still can use method without specifying both arguments and can invoke it like that:

adapterView.onItemSelected<ITEM> { /* action */ }

I'd agree that onNothingSelected is rarely used but getting rid of of it completely as in your implementation proposition might not be the best options. Especially that we can have concise and general method that handle both arguments.

@fada21
Copy link
Author

fada21 commented Apr 26, 2018

PR updated and ready for review.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants