Commit 72b40fdf by Paktalin

Extracted ViewHolder into a separate class

parent 8e81ce92
package com.paktalin.vocabularynotebook
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.word_item.view.*
class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val tvWord: TextView = itemView.word
private val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
fun init(wordItem: WordItem, position: Int, showPopupMenu: (View, Int) -> Unit) {
tvWord.text = wordItem.pojo.word
tvTranslation.text = wordItem.pojo.translation
itemView.setOnClickListener { showPopupMenu(itemView, position) }
}
}
...@@ -14,7 +14,7 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity ...@@ -14,7 +14,7 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.addFragment import com.paktalin.vocabularynotebook.utils.addFragment
import kotlinx.android.synthetic.main.word_item.view.* import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<Holder>() {
private var displayedVocabulary = Vocabulary(fullVocabulary) private var displayedVocabulary = Vocabulary(fullVocabulary)
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
...@@ -30,33 +30,30 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main ...@@ -30,33 +30,30 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main
mainActivity.searchView.setOnQueryTextListener(OnQueryTextListener(this@VocabularyAdapter)) mainActivity.searchView.setOnQueryTextListener(OnQueryTextListener(this@VocabularyAdapter))
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(parent.context) val view = LayoutInflater.from(parent.context).inflate(R.layout.word_item, parent, false)
.inflate(R.layout.word_item, parent, false) return Holder(view)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = displayedVocabulary.getAt(position)
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
//todo set click listener to menu
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return displayedVocabulary.size() return displayedVocabulary.size()
} }
override fun onBindViewHolder(holder: Holder, position: Int) {
val wordItem = displayedVocabulary.getAt(position)
holder.init(wordItem, position) { view, p -> showPopupMenu(view, p)}
//todo set click listener to menu
}
private fun showPopupMenu(v: View, position: Int) { private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(mainActivity, v) val popup = PopupMenu(mainActivity, v)
val inflater = popup.menuInflater popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener { popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWord(position) } if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { startEditFragment(v, displayedVocabulary.getAt(position)) } if (it.itemId == R.id.option_edit) { startEditFragment(v, displayedVocabulary.getAt(position)) }
true true
} }
// if not edit mode, then start EditFragment
if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null) if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show() popup.show()
} }
...@@ -105,12 +102,6 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main ...@@ -105,12 +102,6 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment") addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment")
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.word
val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
}
fun filter(query: String) { fun filter(query: String) {
displayedVocabulary.clear() displayedVocabulary.clear()
if (query.isEmpty()) if (query.isEmpty())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment