Commit 839416e6 by Paktalin

An empty field added to the recycler view

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