SimpleAdapter is used to eliminate boilerplate code for create different class for default RecyclerView.Adapter with help for databinding.
Project level build.gradle file
maven { url 'https://jitpack.io' }
Include the following dependency in your apps build.gradle file.
implementation 'com.github.pankaj89:SimpleAdapter:3.4.1'
val adapter = SimpleAdapter.with<User, ItemBinding>(R.layout.item) { adapterPosition, model, binding ->
binding.text.text = model.name
}
recyclerView.adapter = adapter
You will get adapter position, model and binding simply bind your model with binding
val list = ArrayList<User>()
for (i in 0..100) {
list.add(User("$i"))
}
adapter2.addAll(list)
adapter2.notifyDataSetChanged()
adapter.addViewType<Item1Binding>(R.layout.item_1, binder = { adapterPosition, model, binding ->
binding.text1.text = model.name
}, viewTypeLogic = { model ->
//Return true if received model is your view type
model.name.toInt() % 5 == 0
})
Here ViewTypeLogic is callback where you need to return true/false based on your type
adapter.setClickableViews({ view, model, adapterPosition ->
Toast.makeText(this@MyActivity, "${model.name} clicked", Toast.LENGTH_SHORT).show()
}, R.id.text, R.id.text1, R.id.text2)
adapter.enableLoadMore(recyclerView) {
//Call WS here to load more item after items added call setLoadMoreComplete(), If there are more data to load return `true` else `false`
true
}
- Return true when you need to load more items.
- When subsequent page are added to adapter call setLoadMoreComplete()
adapter.performFilter(text = "", filterLogic = { text, model ->
//Return true if this model is applicable for your filtering
model.name.contains(text)
})
- Return true when you need to load more items.
Copyright 2017 Pankaj Sharma
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.