Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
vocabulary_notebook
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
72b40fdf
authored
Apr 19, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracted ViewHolder into a separate class
parent
8e81ce92
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
21 deletions
app/src/main/java/com/paktalin/vocabularynotebook/Holder.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/Holder.kt
0 → 100644
View file @
72b40fdf
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
)
}
}
}
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
72b40fdf
...
...
@@ -14,7 +14,7 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.utils.addFragment
import
kotlinx.android.synthetic.main.word_item.view.*
class
VocabularyAdapter
(
private
val
fullVocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
View
Holder
>()
{
class
VocabularyAdapter
(
private
val
fullVocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
Holder
>()
{
private
var
displayedVocabulary
=
Vocabulary
(
fullVocabulary
)
private
lateinit
var
recyclerView
:
RecyclerView
...
...
@@ -30,33 +30,30 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main
mainActivity
.
searchView
.
setOnQueryTextListener
(
OnQueryTextListener
(
this
@VocabularyAdapter
))
}
override
fun
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
ViewHolder
{
val
view
=
LayoutInflater
.
from
(
parent
.
context
)
.
inflate
(
R
.
layout
.
word_item
,
parent
,
false
)
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
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
Holder
{
val
view
=
LayoutInflater
.
from
(
parent
.
context
).
inflate
(
R
.
layout
.
word_item
,
parent
,
false
)
return
Holder
(
view
)
}
override
fun
getItemCount
():
Int
{
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
)
{
val
popup
=
PopupMenu
(
mainActivity
,
v
)
val
inflater
=
popup
.
menuInflater
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
menuInflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWord
(
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
startEditFragment
(
v
,
displayedVocabulary
.
getAt
(
position
))
}
true
}
// if not edit mode, then start EditFragment
if
(
mainActivity
.
supportFragmentManager
.
findFragmentByTag
(
"edit_fragment"
)
==
null
)
popup
.
show
()
}
...
...
@@ -105,12 +102,6 @@ class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val main
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
)
{
displayedVocabulary
.
clear
()
if
(
query
.
isEmpty
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment