Skip to content

Multiple Type Item for RecycleView on Android with Annotation Processor

Notifications You must be signed in to change notification settings

dangngocduc/Leso

Repository files navigation

Installation

Add this to your root build.gradle file under repositories

allprojects {
    repositories {
        maven {
            url "https://dl.bintray.com/dangngocduc/AndroidLibrary"
        }
    }
}

Add this to your app level build.gradle as dependency

    compile 'com.android.leso:leso:1.1.7'
    annotationProcessor 'com.android.leso:processor:1.2.0'

with kotlin

    apply plugin: 'kotlin-kapt'
    ....
    dependencies {
        compile 'com.android.leso:leso:1.1.7'
        kapt 'com.android.leso:processor:1.2.0'
    }

Implementation

Step 1 : Create ViewHolder for RecycleView

@ViewHolder(layout = R.layout.row_simple_title2, data = Title2::class)
class ViewHolderSimple2(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun bindData(context: Context, title: Title2) {
        itemView.title.text = title.titlte
        itemView.subTitle.text = title.subTitle
    }
}
@ViewHolder(layout = R.layout.row_simple_title, data = Title::class)
class ViewHolderSimpleTitle(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun bindData(context: Context, title: Title) {

        itemView.title.text = title.titlte
    }
}

Step 2 : Create Adapter RecycleView

@AdapterRecycleView(viewholders = [(ViewHolderSimpleTitle::class), (ViewHolderSimple2::class)])
class HomeAdapter(var context :Context) : HomeAdapter_Builder<Any>(context)  {
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(holder) {
            is ViewHolderSimpleTitle -> {
                holder.bindData(context, mDatas[position] as Title)
            }
            is ViewHolderSimple2 -> {
                holder.bindData(context, mDatas[position] as Title2)
            }
        }

    }
}

About

Multiple Type Item for RecycleView on Android with Annotation Processor

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published