SimpleGenericAdapter

Additional

Language
Kotlin
Version
0.3.0 (Sep 9, 2019)
Created
Aug 7, 2019
Updated
Sep 9, 2019
Owner
Tristan Le (trieulh-ict)
Contributor
Tristan Le (trieulh-ict)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertising

SimpleGenericAdapter

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


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 fun getType(): Int = ItemType.ADVERTISEMENT.value

    override fun isStickyModule(item: Advertisement): Boolean = true

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

Add Sticky/Non-sticky Header

  • To use Header, you can implement HeaderModule class:
class HeaderEmployeeModule(private var isSticky: Boolean = false) : HeaderModule() {
    override val layoutRes: Int = R.layout.item_header

    override fun onBind(holder: SimpleViewHolder) {
        //Do nothing now
    }

    override fun isStickyModule(): Boolean = isSticky
}
  • You can set header as sticky item by overriding isStickyModule() method

Create Sticky/Non-sticky Item

  • To set normal item as Sticky one, you can implement ItemModule class as usual, then override method isStickyModule() to true:
class AdvertisementModule : ItemModule<Advertisement>() {
    ...
    override fun isStickyModule(item: Advertisement): Boolean = true
    ...
}