Commit cd16c2cd by Paktalin

Popup menu is added to every vocabulary item

parent 9b0bdf06
package com.paktalin.vocabularynotebook
import android.content.Context
import android.app.Activity
import android.content.Intent
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
private val context: Context) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
......@@ -31,6 +33,7 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
holder.tvTranslation.text = wordItem.pojo!!.translation
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) }
holder.itemView.setOnLongClickListener { deleteWordItem(position);true }
holder.btnPopupMenu.setOnClickListener { showPopupMenu(holder.btnPopupMenu) }
}
override fun getItemCount(): Int {
......@@ -40,6 +43,7 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.tvWord)
val tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation)
val btnPopupMenu: ImageButton = itemView.findViewById(R.id.btnContextMenu)
}
private fun openWordItemInfo(wordItem: WordItem) {
......@@ -48,6 +52,13 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
context.startActivity(intentWordItemInfo)
}
private fun showPopupMenu(v: View) {
val popup = PopupMenu(context, v)
val inflater = popup.menuInflater
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.show()
}
private fun deleteWordItem(position: Int) {
wordItems[position].delete()
wordItems.removeAt(position)
......
......@@ -38,6 +38,7 @@ class AddWordActivity : AppCompatActivity() {
.collection(WORDS).add(WordItemPojo(word, translation)).addOnSuccessListener {
Log.i(TAG, "Successfully added a new word $word")
clearFields()
cancel()
}
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
......
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0, 1">
android:stretchColumns="0, 1, 2">
<TableRow android:padding="8dp">
......@@ -20,5 +21,12 @@
android:layout_height="wrap_content"
android:text="translation"
android:textSize="22sp" />
<ImageButton
android:id="@+id/btnContextMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_edit" />
</TableRow>
</TableLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Item" />
<item android:title="Item2" />
</menu>
\ No newline at end of file
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