ViewHolderArrayAdapter
ArrayAdapter which follows the RecyclerView API
Install / Use
/learn @Commit451/ViewHolderArrayAdapterREADME
ViewHolderArrayAdapter
A replacement of ArrayAdapter which makes use of ViewHolders from the RecyclerView API.
Dependency
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
and within your module's build.gradle
dependencies {
implementation 'com.github.Commit451:ViewHolderArrayAdapter:insert.latest.version'
}
Usage
See the sample project for full usage.
Typically, you will create a ViewHolderArrayAdapter:
val spinner = findViewById<Spinner>(R.id.spinner)
val adapter = ViewHolderArrayAdapter(
context = this,
objects = cheeses,
onCreateViewHolder = { parent ->
// create the overall view holder
CheeseViewHolder.inflate(parent)
},
onCreateDropDownViewHolder = { parent ->
// create the dropdown view holder
CheeseDropDownViewHolder.inflate(parent)
},
onBindViewHolder = { viewHolder, position, item ->
// bind the overall view holder
viewHolder.bind(item)
},
onBindDropDownViewHolder = { viewHolder, position, item ->
// bind the dropdown view holder
viewHolder.bind(item)
}
)
spinner.adapter = adapter
If you want to use the same ViewHolder for both the dropdown and the selected item, you can use SingleViewHolderArrayAdapter. And if you have a simple view where only the text changes, you can use TextViewHolderArrayAdapter, or SingleTextViewHolderArrayAdapter
ViewHolderArrayAdapter is not just limited to Spinners, though that is what it was originally designed for. All in all, you could use it with:
- Spinner
- AutoCompleteTextView
- ListView (please don't use ListView)
Why ViewHolder?
Many times, you might have ViewHolders defined that can be shared between Spinners and RecyclerViews. This simple library bridges the gap. Also using ViewHolder is more efficient, as it forces recycling for spinners which can otherwise be overlooked.
License
Copyright 2020 Commit 451
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.
Related Skills
node-connect
351.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
