Commit 839416e6 by Paktalin

An empty field added to the recycler view

parent 4decc79b
package com.paktalin.vocabularynotebook package com.paktalin.vocabularynotebook
import android.content.Context import android.content.Context
import android.os.Build
import android.text.TextUtils import android.text.TextUtils
import android.widget.EditText
import android.widget.Toast import android.widget.Toast
class Utils { class Utils {
...@@ -13,5 +15,14 @@ class Utils { ...@@ -13,5 +15,14 @@ class Utils {
} }
return true return true
} }
fun setEmptyEditText(et:EditText, hint:String) {
et.isClickable = true
et.isFocusable = true
et.isFocusableInTouchMode = true
et.isCursorVisible = true
et.hint = hint
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) et.background = null
}
} }
} }
\ No newline at end of file
...@@ -2,17 +2,20 @@ package com.paktalin.vocabularynotebook ...@@ -2,17 +2,20 @@ package com.paktalin.vocabularynotebook
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Build
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.PopupMenu import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageButton import android.widget.*
import android.widget.TextView
import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
...@@ -28,12 +31,18 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, ...@@ -28,12 +31,18 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = wordItems[position] if (position == 0) showEmptyItem(holder)
holder.tvWord.text = wordItem.pojo!!.word else {
holder.tvTranslation.text = wordItem.pojo!!.translation val wordItem = wordItems[position]
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) } holder.etWord.setText(wordItem.pojo!!.word)
holder.btnPopupMenu.setOnClickListener { showPopupMenu(holder.btnPopupMenu, position) } holder.etTranslation.setText(wordItem.pojo!!.translation)
//todo set click listener to menu
holder.layout.setOnClickListener{ openWordItemInfo(wordItem) }
holder.etWord.setOnClickListener{ openWordItemInfo(wordItem) }
holder.etTranslation.setOnClickListener{ openWordItemInfo(wordItem) }
holder.btnPopupMenu.setOnClickListener { showPopupMenu(holder.btnPopupMenu, position) }
//todo set click listener to menu
}
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
...@@ -41,9 +50,10 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, ...@@ -41,9 +50,10 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.tvWord) val etWord: EditText = itemView.findViewById(R.id.etWord)
val tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation) val etTranslation: EditText = itemView.findViewById(R.id.etTranslation)
val btnPopupMenu: ImageButton = itemView.findViewById(R.id.btnContextMenu) val btnPopupMenu: ImageButton = itemView.findViewById(R.id.btnContextMenu)
val layout: LinearLayout = itemView.findViewById(R.id.tableLayout)
} }
private fun openWordItemInfo(wordItem: WordItem) { private fun openWordItemInfo(wordItem: WordItem) {
...@@ -70,5 +80,24 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, ...@@ -70,5 +80,24 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
this.notifyItemRangeChanged(position, wordItems.size) this.notifyItemRangeChanged(position, wordItems.size)
} }
private fun showEmptyItem(holder: ViewHolder) {
holder.btnPopupMenu.isClickable = false
holder.btnPopupMenu.visibility = View.INVISIBLE
Utils.setEmptyEditText(holder.etWord, "new word")
Utils.setEmptyEditText(holder.etTranslation, "translation")
holder.etWord.setOnFocusChangeListener({ _, focus ->
if (focus) showCancelButton()
})
holder.etTranslation.setOnFocusChangeListener({ _, focus ->
if (focus) showCancelButton()
})
}
private fun showCancelButton() {
Log.d(TAG, "empty word is focused")
}
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName } companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
init { wordItems.add(0, WordItem.createEmpty()) }
} }
\ No newline at end of file
...@@ -9,29 +9,29 @@ import com.google.firebase.firestore.FirebaseFirestore ...@@ -9,29 +9,29 @@ import com.google.firebase.firestore.FirebaseFirestore
import java.io.Serializable import java.io.Serializable
class WordItem( class WordItem(word: String?, translation: String?, var id: String?, private val vocabularyId: String?) : Serializable {
word: String,
translation: String,
var id: String?,
private val vocabularyId: String) : Serializable {
var pojo: WordItemPojo? = null var pojo: WordItemPojo? = null
class WordItemPojo(var word: String?, class WordItemPojo(var word: String?, var translation: String?) : Serializable
var translation: String?) : Serializable
init { init {
this.pojo = WordItemPojo(word, translation) this.pojo = WordItemPojo(word, translation)
} }
fun delete() { fun delete() {
FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId) if (vocabularyId != null) {
.collection("words").document(id!!).delete() FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId)
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") } .collection("words").document(id!!).delete()
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) } .addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
} }
companion object { companion object {
private val TAG = "VN/" + WordItem::class.java.simpleName private val TAG = "VN/" + WordItem::class.java.simpleName
fun createEmpty() : WordItem {
return WordItem(null, null, null, null)
}
} }
} }
...@@ -40,8 +40,8 @@ class WordItemInfoActivity: AppCompatActivity() { ...@@ -40,8 +40,8 @@ class WordItemInfoActivity: AppCompatActivity() {
} }
private fun setData() { private fun setData() {
tvWord.text = wordItem.pojo!!.word etWord.text = wordItem.pojo!!.word
tvTranslation.text = wordItem.pojo!!.translation etTranslation.text = wordItem.pojo!!.translation
} }
private fun cancel() { private fun cancel() {
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<include layout="@layout/img_background"/> <include layout="@layout/img_background"/>
<TextView <TextView
android:id="@+id/tvWord" android:id="@+id/etWord"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/tvTranslation" android:id="@+id/etTranslation"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
...@@ -27,6 +27,6 @@ ...@@ -27,6 +27,6 @@
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="Translation" android:text="Translation"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvWord" /> app:layout_constraintTop_toBottomOf="@+id/etWord" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TableLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:stretchColumns="0, 1, 2"> android:orientation="horizontal"
android:clickable="true"
<TableRow android:padding="8dp"> android:focusable="true"
android:focusableInTouchMode="true"
android:padding="8dp">
<TextView <EditText
android:id="@+id/tvWord" android:id="@+id/etWord"
android:layout_width="92dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="word" android:layout_weight="8"
android:textSize="22sp" /> android:background="@android:color/transparent"
android:cursorVisible="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="22sp"
tools:ignore="LabelFor"
android:inputType="text" />
<TextView <EditText
android:id="@+id/tvTranslation" android:id="@+id/etTranslation"
android:layout_width="145dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="translation" android:layout_weight="8"
android:textSize="22sp" /> android:background="@android:color/transparent"
android:cursorVisible="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="22sp"
tools:ignore="LabelFor"
android:inputType="text"/>
<ImageButton <ImageButton
android:id="@+id/btnContextMenu" android:id="@+id/btnContextMenu"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/transparent" android:layout_weight="1"
app:srcCompat="@drawable/ic_delete_icon" /> android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_delete_icon"
tools:ignore="ContentDescription" />
</TableRow> </LinearLayout>
</TableLayout> \ No newline at end of file
\ 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