/ RecyclerView

A Simplified Adapter for RecyclerView

A Simplified Adapter for RecyclerView

SimpleGenericAdapter

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

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([email protected], "${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:

itemAnim

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:

full

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

partial

  • 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.

GitHub