generic-adapter-kotlin

Additional

Language
Kotlin
Version
N/A
Created
Feb 15, 2018
Updated
Aug 31, 2018
Owner
Jallu (MaximeJallu)
Contributor
Jallu (MaximeJallu)
1
Activity
Badge
Generate
Download
Source code

Show card

Status

Generic-Adapter-Kotlin:

This tool allows you to no longer worry about adapters. Now you will only create your ViewHolder. Communication management between your Views and your ViewHolders is possible. Creating sections is now very easily. Enjoy.

Download

buildtool used is 27 use {exclude group: 'com.android.support'} only if you have problems

dependencies {
    ...
    implementation ('com.github.maximejallu:adapters-kotlin:18.08.01')
    ...
}

Decorators for : ButterKnife - Picasso - Glide ...

class SampleApplication: Application {

    override 
    fun onCreate() {
        super.onCreate()

        RecyclerAdapter.Companion.Helper()
                    .attachInitBindingView(decorator = object : InitBindingViewHolder {
                        override fun initBinding(target: Any, view: View) {
                            if (view.javaClass.simpleName.contains(".java")) {
                                //TODO e.g. butter-knife binding...
                            }
                        }
                    })
    }
}

1- Create yours RecyclerViewHolder (Easy sample method)

class ViewOne(parent: ViewGroup) : RecyclerViewHolder<Int>(
            parent, R.layout.support_simple_spinner_dropdown_item
        ) {

        init {
            itemView.setOnClickListener {
                (getCommunication() as ViewOneListener).onClick(item)
            }
        }
       
        override fun onBind() {
            itemView.myTextViewId.text = "text: $item"
        }

        interface ViewOneListener: RecyclerViewListener{
            fun onClick(item: Int)
        }  
    }

2 - Create and init Adapter

fun sample(){
val adapter = RecyclerAdapter<Int>(
            items = ArrayList(),
            defaultViewHolder = ViewOne::class, /*for getItemViewType == 0*/
            listener = object :ViewOne.ViewOneListener{
                            override fun onClick(item: Int) {
                                TODO("not implemented")
                            }
            
                        }
        )
    /*attache others viewHolder*/    
    adapter.registerViewHolder(TYPE_2, ViewTwo::class, listener = null)      
} 

3 - Declare strategy view type is necessary (Multi cell method)

fun multicellSample(){
        /*sample for multi Cell [3 viewholder resgistered for this sample]*/
        adapter.setItemViewTypeStrategy(strategy = object : ViewTypeStrategy<Int> {
            override fun getItemViewType(position: Int, item: Int): Int {
                return when{
                    position == 0 -> TYPE_1
                    item > 5 -> TYPE_2
                    else -> TYPE_3
                }
            }
        })
}