mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-06 01:16:23 -06:00
Refactor OnLongClickListener interface to support multiple click-types
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.embers.simpleto_do
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.View
|
||||||
|
|
||||||
|
abstract class DoubleClickListener : View.OnClickListener {
|
||||||
|
private var lastClick: Long = 0
|
||||||
|
|
||||||
|
override fun onClick(v: View) {
|
||||||
|
val curClick = System.currentTimeMillis()
|
||||||
|
if (curClick - lastClick < DOUBLE_CLICK_TIME_DELTA)
|
||||||
|
onDoubleClick(v)
|
||||||
|
lastClick = curClick
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract fun onDoubleClick(item: View)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 // Milliseconds
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package com.embers.simpleto_do
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.KeyEvent
|
import android.view.KeyEvent
|
||||||
import android.view.View
|
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
@@ -68,8 +67,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
/**
|
/**
|
||||||
* Callback to remove a task at a given location in the list (on long click action)
|
* Callback to remove a task at a given location in the list (on long click action)
|
||||||
*/
|
*/
|
||||||
val onLongClickListener = object : TaskItemAdapter.OnLongClickListener {
|
val clickListener = object : TaskItemAdapter.ClickListener {
|
||||||
override fun onItemLongClicked(position: Int) {
|
override fun onLongClick(position: Int) {
|
||||||
taskList.removeAt(position)
|
taskList.removeAt(position)
|
||||||
adapter.notifyItemRemoved(position)
|
adapter.notifyItemRemoved(position)
|
||||||
saveData()
|
saveData()
|
||||||
@@ -79,7 +78,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// Setup RecyclerView
|
// Setup RecyclerView
|
||||||
editText = findViewById<EditText>(R.id.editTodoText)
|
editText = findViewById<EditText>(R.id.editTodoText)
|
||||||
recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||||
adapter = TaskItemAdapter(taskList, onLongClickListener)
|
adapter = TaskItemAdapter(taskList, clickListener)
|
||||||
recyclerView.adapter = adapter
|
recyclerView.adapter = adapter
|
||||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||||
button = findViewById(R.id.button)
|
button = findViewById(R.id.button)
|
||||||
@@ -87,13 +86,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
refreshButton()
|
refreshButton()
|
||||||
|
|
||||||
// Disable add task button when text is not inside EditText
|
// Disable add task button when text is not inside EditText
|
||||||
findViewById<EditText>(R.id.editTodoText).setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
|
findViewById<EditText>(R.id.editTodoText).setOnKeyListener { _, _, event ->
|
||||||
// Read only event after the action has been processed in the textbox
|
// Read only event after the action has been processed in the textbox
|
||||||
if (event.action == KeyEvent.ACTION_UP)
|
if (event.action == KeyEvent.ACTION_UP)
|
||||||
refreshButton()
|
refreshButton()
|
||||||
|
|
||||||
false
|
false
|
||||||
})
|
}
|
||||||
|
|
||||||
findViewById<Button>(R.id.button).setOnClickListener {
|
findViewById<Button>(R.id.button).setOnClickListener {
|
||||||
// Check that the task name has text in it
|
// Check that the task name has text in it
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ import androidx.recyclerview.widget.RecyclerView
|
|||||||
|
|
||||||
class TaskItemAdapter(
|
class TaskItemAdapter(
|
||||||
private val taskList: List<String>,
|
private val taskList: List<String>,
|
||||||
private val longClickListener: OnLongClickListener
|
private val clickListener: ClickListener
|
||||||
) :
|
) :
|
||||||
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
||||||
|
|
||||||
interface OnLongClickListener {
|
interface ClickListener {
|
||||||
fun onItemLongClicked(position: Int)
|
fun onSingleClick(position: Int) {}
|
||||||
|
fun onDoubleClick(position: Int) {}
|
||||||
|
fun onLongClick(position: Int) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||||
@@ -21,9 +23,15 @@ class TaskItemAdapter(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
itemView.setOnLongClickListener {
|
itemView.setOnLongClickListener {
|
||||||
longClickListener.onItemLongClicked(bindingAdapterPosition)
|
clickListener.onLongClick(bindingAdapterPosition)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itemView.setOnClickListener(object : DoubleClickListener() {
|
||||||
|
override fun onDoubleClick(item: View) {
|
||||||
|
clickListener.onDoubleClick(bindingAdapterPosition)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,10 +22,10 @@
|
|||||||
android:layout_margin="10dp"
|
android:layout_margin="10dp"
|
||||||
android:importantForAutofill="no"
|
android:importantForAutofill="no"
|
||||||
android:inputType="textShortMessage"
|
android:inputType="textShortMessage"
|
||||||
tools:labelFor="@string/textbox_labelfor"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/button"
|
app:layout_constraintEnd_toStartOf="@+id/button"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
tools:labelFor="@string/textbox_labelfor" />
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/recyclerView"
|
android:id="@+id/recyclerView"
|
||||||
|
|||||||
Reference in New Issue
Block a user