This is a library that abstracts, and completely hide the Adapter and ViewHolder class creation part. It is general purpose object that can be customized to each case (item layout) by the time of creation. Similar to what is done on Listview with default adapters. Compatible with androidx.
This library can be implemented on any android project with minimum API 14. You must have a list and an item_layout for each item of the list
Lets start by adding a corresponding repository in your root build.gradle
file. (prefer below all other)
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
The next task is to add the dependency to your app build.gradle
file.
dependencies {
...
implementation 'com.github.horaciocome1:simple-recyclerview-adapter:0.1.3'
}
Now you ready to go. Except that you should sync your project first.
As mention on IO18, android support libraries will not be updated anymore, that's the main reason ive moved to androidx and new material design libraries. That's why if you have any appcompat or design support library as dependency the build will fail. Because the androidx on these lib will conflict with android support on your app. I am wondering if it is necessary to do a separate lib for support appcompat. Email me if you thinking on that.
Call the default constructor passing as Datatype the class of your list objects.
recyclerView.adapter = SimpleRecyclerViewAdapter<DataType>().apply {
itemLayout = R.layout.your_item_layout
list = yourList
setOnBindViewHolder { holder, dataType -> /* bind data here */ }
}
Lets have a look at some common binding examples
recyclerView.adapter = SimpleRecyclerViewAdapter<User>().apply {
itemLayout = R.layout.item_user
list = users
setOnBindViewHolder { holder, user ->
holder.findViewById<Textview>(R.id.name).text = user.name
holder.findViewById<Textview>(R.id.age).text = user.age
ImageLoader.with(context)
.load(user.photo)
.into(holder.findViewById<ImageView>(R.id.photo))
}
}
SimpleRecyclerViewAdapter<DataType> adapter = new SimpleRecyclerViewAdapter<>();
adapter.setItemLayout(R.layout.your_item_layot);
adapter.setList(yourList);
adapter.setOnBindViewHolder(new Function2<SimpleRecyclerViewAdapter.ViewHolder, DataType, Unit>() {
@Override
public Unit invoke(SimpleRecyclerViewAdapter.ViewHolder viewHolder, DataType data) {
// bind data here
}
});
recyclerView.setAdapter(adapter);
No casting need. I suggest you to use Android Studio code completion, to implement onBindViewHolder().
adapter.addItem(item)
adapter.setItem(item, position)
adapter.removeItem(position)
adapter.restoreItem(item, position)
Notice that these calls might require public adapter object/variable
Naturally, the adapter needs an item_layout, an list and an onBindViewHolder() implementation to display the list on screen. NONE OF THIS ARE OPTIONAL, so not setting them may lead to runtime app crash.
// this is what you need to a void
recyclerView.adapter = SimpleRecyclerViewAdapter<DataType>()
This is might be a dependency matter. Please reference to the part on the start where i talked about support libraries.
Copyright 2018 Horácio Flávio Comé Júnior
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.
I am open to suggestions of any kind. Please be expressive, so others so we'all will be able to understand each other! Report any issues, please!
This is part of a serie of libraries that pretend to make recyclerview usage more easy. For a touch listener please see Simple RecyclerView Touch Listener