Last active
October 19, 2018 23:03
-
-
Save liberaid2/a0eecc5eca665641cac2176afd17f06b to your computer and use it in GitHub Desktop.
RecyclerView adapter template for MVP pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liberaid.mvprecyclerview.recyclerview | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
/** | |
* RecyclerView adapter template for MVP pattern | |
* This allows us to separate data source, android-related methods and adapter functionality | |
* According to this structure RVAdapter corresponds only to mapping data to views | |
* It has no access to data | |
* | |
* @property listener - adapter's callbacks handler, actually presenter of parent structure (e.g. activity or fragment) | |
* @property layoutID - id of layout to inflate in recyclerView | |
* */ | |
class RVAdapter(private val listener: IListener, private val layoutID: Int) : RecyclerView.Adapter<RVAdapter.ViewHolder>(){ | |
/** | |
* Contains default callbacks | |
* */ | |
interface IListener { | |
fun getItemCount(): Int | |
fun onBindViewOnPosition(view: IElementView, position: Int) | |
} | |
/** | |
* Actually view interface in MVP pattern | |
* | |
* This interface should contains methods to update views | |
* in recyclerView's elements | |
* */ | |
interface IElementView | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val view = LayoutInflater.from(parent.context).inflate(layoutID, parent, false) | |
return ViewHolder(view) | |
} | |
override fun getItemCount() = listener.getItemCount() | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) = listener.onBindViewOnPosition(holder, position) | |
/** | |
* Basic implementation of viewHolder | |
* */ | |
class ViewHolder(mainView: View) : RecyclerView.ViewHolder(mainView), IElementView | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment