RecyclerViewPresenter

Additional

Language
Kotlin
Version
1.5.2 (Dec 25, 2016)
Created
May 15, 2016
Updated
Apr 12, 2019
Owner
Jan Rabe (kibotu)
Contributor
Jan Rabe (kibotu)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

RecyclerView Presenter

Convenience library to handle different view types with different presenters in a single RecyclerView.

How to install

repositories {
    maven {
        url "https://jitpack.io"
    }
}
 
dependencies {
    implementation 'com.github.kibotu:RecyclerViewPresenter:-SNAPSHOT'
}

How to use

  1. Add the PresenterAdapter to your RecyclerView

     val adapter = PresenterAdapter<RecyclerViewModel<String>>()
     list.adapter = adapter
    
  2. Add a model with a Presenter as representation to the adapter, e.g.:

     adapter.append(RecyclerViewModel(myModelObject), PhotoPresenter::class.java)
     adapter.append(RecyclerViewModel(myModelObject), LabelPresenter::class.java)
    
  3. Create a presenter, e.g. PhotoPresenter or LabelPresenter

     class PhotoPresenter : Presenter<RecyclerViewModel<String>>() {
    
         override val layout: Int = R.layout.photo_presenter_item
    
         override fun bindViewHolder(viewHolder: RecyclerView.ViewHolder, item: RecyclerViewModel<String>, position: Int): Unit = with(viewHolder.itemView) {
    
         }
     }
    
  4. Add click listener to adapter

     adapter.onItemClick { item, view, position ->
         toast("$position. ${item.model}")
     }
    

4.1 or pass to your RecyclerViewModel

    RecyclerViewModel(
        model = createRandomImageUrl(),
        onItemClickListener = { item, view, position ->
            toast("$position. $item")
        }
    )

Updating item

 adapter.update(0, RecyclerViewModel(myNewModel))

License

Copyright 2019 Jan Rabe

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.