Skip to content

Commit

Permalink
Update onEditorAction callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianzhu Qiao committed Nov 23, 2020
1 parent 2cc2f8e commit 76045da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/feiyilin/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class MainActivity : FormActivity() {
.inputType(EditorInfo.TYPE_CLASS_NUMBER)
+FormItemText().title("Phone").tag("phone")
.inputType(EditorInfo.TYPE_CLASS_PHONE).imeOptions(EditorInfo.IME_ACTION_PREVIOUS)
.onEditorAction {item, _, _ ->
Toast.makeText(this@MainActivity, "EditorAction on ${item.title}", Toast.LENGTH_SHORT).show()
false
}
+FormItemPassword().title("Password").tag("password")
+FormItemLabel().title("Label").tag("label")
.subTitle("subtitle of label")
Expand Down Expand Up @@ -377,6 +381,14 @@ class MainActivity : FormActivity() {
}
}
}

override fun onEditorAction(
item: FormItem,
actionId: Int,
viewHolder: RecyclerView.ViewHolder
): Boolean {
return super.onEditorAction(item, actionId, viewHolder)
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions form/src/main/java/com/feiyilin/form/FormItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ open class FormItem {
var onStartReorder: ((viewHolder: RecyclerView.ViewHolder) -> Boolean)? = null
var onMoveItem: ((src: Int, dest: Int) -> Boolean)? = null
var onSwipedAction: ((action: FormSwipeAction, viewHolder: RecyclerView.ViewHolder) -> Boolean)? = null
var onEditorAction: ((actionId: Int, viewHolder: RecyclerView.ViewHolder) -> Boolean)? = null
}

fun <T : FormItem> T.title(title: String) = apply {
Expand Down Expand Up @@ -229,6 +230,17 @@ fun <T : FormItem> T.onSwipedAction(callback: ((item: T, action: FormSwipeAction
}
}

fun <T : FormItem> T.onEditorAction(callback: ((item: T, actionId: Int, viewHolder: RecyclerView.ViewHolder) -> Boolean)?) = apply {
if (callback == null) {
this.onEditorAction = null
} else {
this.onEditorAction = { actionId, viewHolder ->
callback.invoke(this, actionId, viewHolder)
}
}
}


open class FormItemLabel : FormItem() {
}

Expand Down
12 changes: 8 additions & 4 deletions form/src/main/java/com/feiyilin/form/FormRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface FormItemCallback {
return false
}

fun onEditAction(item: FormItem, actionId: Int, viewHolder: RecyclerView.ViewHolder): Boolean {
fun onEditorAction(item: FormItem, actionId: Int, viewHolder: RecyclerView.ViewHolder): Boolean {
return false
}
fun getMinItemHeight(item: FormItem): Int {
Expand Down Expand Up @@ -483,9 +483,13 @@ open class FormRecyclerAdapter(
return listener?.onSwipedAction(item, action, viewHolder) ?: false
}

override fun onEditAction(item: FormItem, actionId: Int, viewHolder: RecyclerView.ViewHolder): Boolean {
super.onEditAction(item, actionId, viewHolder)
if (listener?.onEditAction(item, actionId, viewHolder) == true) {
override fun onEditorAction(item: FormItem, actionId: Int, viewHolder: RecyclerView.ViewHolder): Boolean {
super.onEditorAction(item, actionId, viewHolder)
if (item.onEditorAction != null) {
if (item.onEditorAction?.invoke(actionId, viewHolder) == true) {
return true
}
} else if (listener?.onEditorAction(item, actionId, viewHolder) == true) {
return true
}
when (actionId) {
Expand Down
2 changes: 1 addition & 1 deletion form/src/main/java/com/feiyilin/form/FormViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ open class FormBaseTextViewHolder(inflater: LayoutInflater, resource: Int, paren
}
valueView?.setOnEditorActionListener { v, actionId, _ ->
item?.let {
listener?.onEditAction(it, actionId, this)
listener?.onEditorAction(it, actionId, this)
return@setOnEditorActionListener true
}
false
Expand Down
Binary file added images/item_text_password.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 76045da

Please sign in to comment.