GenericAdapter

Additional

Language
Java
Version
N/A
Created
Apr 5, 2019
Updated
Jun 18, 2019
Owner
Manoj Bhadane (manojbhadane)
Contributor
Manoj Bhadane (manojbhadane)
1
Activity
Badge
Generate
Download
Source code
APK file

Announcement

Android Library

GenericAdapter

An Easy to use adapter for android

  1. No need to create seperate class for adapter
  2. No need of viewholder
  3. More readble code

Supports 2 types of adapters

GenericAdapter : Adapter for simple usage

GenericFilterAdapter : Adapter with list filtering capability

Medium

Please find detail medium post here, Android Generic Adapter

Download

This library is available in jitPack which is the default Maven repository used in Android Studio.

Gradle

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'
}

Usage

  1. In App level build.gradle
dataBinding {
        enabled true
}
  1. 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) {

            }
        });
  1. 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

v1.2

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;
            }
        });

Bugs or Requests

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.

Spread Some ❤️

About The Author

Manoj Bhadane

Android & Backend Developer.

If this library helps you in anyway, show your love ❤️ by putting a ⭐️ on this project ✌️

License

MIT License

Copyright (c) 2019 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.