Skip to content
A Simplified Adapter for RecyclerView
Kotlin Java
Branch: master
Clone or download
Latest commit dd0e91c Aug 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea 0.2.0: Update README Aug 9, 2019
.settings 0.2.0: Update README Aug 9, 2019
gradle/wrapper Init library Aug 7, 2019
images 0.2.0: Update README Aug 9, 2019
sample 0.2.0: Update README Aug 9, 2019
simplegenericadapter
.gitignore Update gitignore Aug 9, 2019
.project 0.2.0: Update README Aug 9, 2019
LICENSE Initial commit Aug 7, 2019
README.md Merge v0.2.0 (#2) Aug 9, 2019
build.gradle Update README and sample Aug 7, 2019
gradle.properties Init library Aug 7, 2019
gradlew Init library Aug 7, 2019
gradlew.bat Init library Aug 7, 2019
settings.gradle Init library Aug 7, 2019

README.md

SimpleGenericAdapter

Download

SimpleGenericAdapter is an Android library that helps developers to easily create a Recyclerview Adapter without repeatedly building any adapter boilerplate.

Features

v0.2.0

  • Support Scroll Item Animation
  • Add Drag & Drop feature

v0.1.0

  • Create UI Module to bind item data
  • Create UI Module for Empty state
  • Create UI Module for Paging
  • Support OnItemClickListener

Demo

How to use it?

Setup

On your module's build.gradle file add this implementation statement to the dependencies section:

dependencies {
  implementation 'com.github.trieulh-ict:SimpleGenericAdapter:x.x.x'
}

Also make sure that Project module includes jitpack:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Create Modules

  • Implement an Item Module using ItemModule<ModelType> (ModelType class should implement Diffable)
class AdvertisementModule : ItemModule<Advertisement>() {
    override val layoutRes: Int = R.layout.item_ad

    override val viewType: Int = ItemType.ADVERTISEMENT.value

    override fun onBind(item: Advertisement, holder: SimpleViewHolder) {
            holder.itemView.findViewById<AppCompatTextView>(R.id.text_content).text = item.content
    }

    override fun isModule(item: Diffable): Boolean {
        return item is Advertisement
    }
}
  • Implement an Empty Module using
class EmployeeEmptyModule : EmptyModule() {

    override val layoutRes: Int = R.layout.item_no_employee

    override fun onBind(holder: SimpleViewHolder) {
        //Do nothing now
    }
}
  • Create adapter and attach to your reyclerview:
adapter = SimpleGenericAdapter.Builder()
            .addItemModule(AdvertisementModule())
            .addEmptyModule(EmployeeEmptyModule())
            .attachTo(listView)
  • You can create Paging module by defining a class or using anonymous object:
adapter = SimpleGenericAdapter.Builder()
            ...
            .addPagingModule(object : PagingModule() {
                            override fun withVisibleThreshold(): Int = 3
                            override val layoutRes: Int = R.layout.item_loading_employee
                            override fun onLoadMore(currentPage: Int) {
                                //Load new Data and add to adapter using `adapter.setItems()`
                            }

                            override fun onBind(holder: SimpleViewHolder) {
                                //Do nothing now
                            }
                        })
            .attachTo(listView)
  • Currently the library only support OnItemClickerListener:
adapter = SimpleGenericAdapter.Builder()
            ...
            .addItemModule(EmployeeModule().addOnItemSelectedListener(object : OnItemSelectedListener<Employee> {
                            override fun onItemSelected(position: Int, item: Employee) {
                                Toast.makeText(this@MainActivity, "${item.id}", Toast.LENGTH_SHORT).show()
                            }
                        }))
            .attachTo(listView)

Scroll Animation

  • Now support 4 default animations ALPHA_IN, SCALE_IN, SLIDE_IN_BOTTOM and SLIDE_IN_RIGHT:

adapter = SimpleGenericAdapter.Builder()
            ...
            .addItemAnimation(SimpleAnimationType.SLIDE_IN_RIGHT)
            ...
            .attachTo(listView)
  • Or you can create and add your own custom animation xml:
adapter = SimpleGenericAdapter.Builder()
            ...
            .addItemAnimation(R.anim.slide_right)
            ...
            .attachTo(listView)

Drag and Drop Item

  • Activate Drag and Drop by setting mode FULL, PARTIAL, NONE:
adapter = SimpleGenericAdapter.Builder()
            ...
            .setDragAndDropMode(SimpleDragAndDropMode.FULL)
            ...
            .attachTo(listView)
  • FULL means you can long press the whole item to drag and drop:

  • PARTIAL means you can choose which view part of ViewHolder to handle Dragging

  • To enable it, set mode to PARTIAL:
adapter = SimpleGenericAdapter.Builder()
            ...
            .setDragAndDropMode(SimpleDragAndDropMode.PARTIAL)
            ...
            .attachTo(listView)
  • In ItemModule, Set view part:
    override fun onBind(item: Employee, holder: SimpleViewHolder) {
        ...
        holder.setDragAndDropByView(view)
        ...
    }

In the future

The library is still under development,so you can suggest more feature by committing issues to this repository.

Credit

Inspired by OneAdapter of ironSource.

License

MIT License

Copyright (c) 2019 Tristan Le

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.

You can’t perform that action at this time.