An Easy to use adapter for android
- No need to create seperate class for adapter
- No need of viewholder
- More readble code
GenericAdapter : Adapter for simple usage
GenericFilterAdapter : Adapter with list filtering capability
Please find detail medium post here, Android Generic Adapter
This library is available in jitPack which is the default Maven repository used in Android Studio.
Step 1. Add it in your root build.gradle at the end of repositories
allprojects
{
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency in your apps module build.gradle
dependencies {
implementation 'com.github.manojbhadane:GenericAdapter:v1.4'
}
- In App level build.gradle
dataBinding {
enabled true
}
- In Activity/Fragment (Java)
mDataBinding.recylerview.setAdapter(new GenericAdapter<PeopleModel, ListitemMainBinding>(this, arrayList) {
@Override
public int getLayoutResId() {
return R.layout.listitem_main;
}
@Override
public void onBindData(PeopleModel model, int position, ListitemMainBinding dataBinding) {
dataBinding.txtName.setText(model.getName());
dataBinding.txtAddress.setText(model.getAddress());
}
@Override
public void onItemClick(PeopleModel model, int position) {
}
});
- In Activity/Fragment (Kotlin)
var adapter = object : GenericAdapter<PeopleModel, ListitemMainBinding>(this, arrayList) {
override fun getLayoutResId(): Int {
return R.layout.listitem_main
}
override fun onBindData(model: PeopleModel?, position: Int, dataBinding: ListitemMainBinding?) {
dataBinding!!.txtName.text = model.getName()
dataBinding!!.txtAddress.text = model.getAddress()
}
override fun onItemClick(model: PeopleModel?, position: Int) {
}
}
dataBinding.recyclerview.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
dataBinding.recyclerview.adapter = adapter
Added list filtering capability to genericAdapter by extending GenericFilterAdapter class
Sample code of adapter with filter
mDataBinding.recylerview.setAdapter(new GenericFilterAdapter<PeopleModel, ListitemMainBinding>(this, arrayList) {
@Override
public int getLayoutResId() {
return R.layout.listitem_main;
}
@Override
public void onBindData(PeopleModel model, int position, ListitemMainBinding dataBinding) {
dataBinding.txtName.setText(model.getName());
dataBinding.txtAddress.setText(model.getAddress());
}
@Override
public void onItemClick(PeopleModel model, int position) {
}
@Override
public View getSearchField() {
return mDataBinding.edtSearch;
}
@Override
public ArrayList<PeopleModel> performFilter(String searchText, ArrayList<PeopleModel> originalList) {
ArrayList<PeopleModel> filteredList = new ArrayList<>();
for (PeopleModel row : originalList) {
if (row.getName().toLowerCase().contains(searchText.toLowerCase())
|| row.getName().contains(searchText)
|| row.getAddress().toLowerCase().contains(searchText.toLowerCase())) {
filteredList.add(row);
}
}
return filteredList;
}
});
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on GitHub and I'll look into it. Pull request are also welcome.
Android & Backend Developer.
MIT License
Copyright (c) 2020 Manoj Bhadane
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.