mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-05 23:16:22 -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.util.Log
|
||||
import android.view.KeyEvent
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
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)
|
||||
*/
|
||||
val onLongClickListener = object : TaskItemAdapter.OnLongClickListener {
|
||||
override fun onItemLongClicked(position: Int) {
|
||||
val clickListener = object : TaskItemAdapter.ClickListener {
|
||||
override fun onLongClick(position: Int) {
|
||||
taskList.removeAt(position)
|
||||
adapter.notifyItemRemoved(position)
|
||||
saveData()
|
||||
@@ -79,7 +78,7 @@ class MainActivity : AppCompatActivity() {
|
||||
// Setup RecyclerView
|
||||
editText = findViewById<EditText>(R.id.editTodoText)
|
||||
recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||
adapter = TaskItemAdapter(taskList, onLongClickListener)
|
||||
adapter = TaskItemAdapter(taskList, clickListener)
|
||||
recyclerView.adapter = adapter
|
||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||
button = findViewById(R.id.button)
|
||||
@@ -87,13 +86,13 @@ class MainActivity : AppCompatActivity() {
|
||||
refreshButton()
|
||||
|
||||
// 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
|
||||
if (event.action == KeyEvent.ACTION_UP)
|
||||
refreshButton()
|
||||
|
||||
false
|
||||
})
|
||||
}
|
||||
|
||||
findViewById<Button>(R.id.button).setOnClickListener {
|
||||
// Check that the task name has text in it
|
||||
|
||||
@@ -8,12 +8,14 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class TaskItemAdapter(
|
||||
private val taskList: List<String>,
|
||||
private val longClickListener: OnLongClickListener
|
||||
private val clickListener: ClickListener
|
||||
) :
|
||||
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
||||
|
||||
interface OnLongClickListener {
|
||||
fun onItemLongClicked(position: Int)
|
||||
interface ClickListener {
|
||||
fun onSingleClick(position: Int) {}
|
||||
fun onDoubleClick(position: Int) {}
|
||||
fun onLongClick(position: Int) {}
|
||||
}
|
||||
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
@@ -21,9 +23,15 @@ class TaskItemAdapter(
|
||||
|
||||
init {
|
||||
itemView.setOnLongClickListener {
|
||||
longClickListener.onItemLongClicked(bindingAdapterPosition)
|
||||
clickListener.onLongClick(bindingAdapterPosition)
|
||||
true
|
||||
}
|
||||
|
||||
itemView.setOnClickListener(object : DoubleClickListener() {
|
||||
override fun onDoubleClick(item: View) {
|
||||
clickListener.onDoubleClick(bindingAdapterPosition)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,10 +22,10 @@
|
||||
android:layout_margin="10dp"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="textShortMessage"
|
||||
tools:labelFor="@string/textbox_labelfor"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
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
|
||||
android:id="@+id/recyclerView"
|
||||
|
||||
Reference in New Issue
Block a user