Last active
June 22, 2018 17:07
-
-
Save phen0menon/39a234458841755485e91d97dd685fb0 to your computer and use it in GitHub Desktop.
Recycler Adapter in Kotlin
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
/** | |
* @param items data which will be displayed by recyclerview | |
* @param context the activity where recyclerview displayed on | |
*/ | |
class Adapter(val items: ArrayList<Object>, val context: Context) : RecyclerView.Adapter<ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false)) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val obj: Object = items[position] | |
holder.textView.text = todo.title | |
holder.bindOnClickListener(context); | |
} | |
override fun getItemCount(): Int { | |
return items.size | |
} | |
} | |
/** | |
* In RecyclerView you can put click event on inflated view inside ViewHolder | |
* class and call it from onBindViewHolder callback method: | |
*/ | |
class ViewHolder (view: View) : RecyclerView.ViewHolder(view) { | |
val textView = view.rvTextViewField | |
fun bindOnClickListener(context: Context) { | |
itemView.setOnClickListener(View.OnClickListener { | |
context.startActivity(Intent(this, SomeClass::class.java)); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment