-1

my application flow:

stockerFragment:

  • input barcode with barcode scanner

  • if barcode already scanned before, scrollsmooth to the existing barcode and show dialog modal to edit quantity.

  • when user hit enter after editing the quantity in dialog modal, dismiss dialog and scrollsmooth to last position. <-- the error happen in this step. it just dismiss the dialog and not auto scrolling to the last position.

this is the code on Qty edittext:

class ModalQtyOnKeyListener(
    private val dialog: Dialog,
    private val QtyTextToEdit: EditText,
    private val QtyModal: EditText,
    private var oldQty: Any,
    private val currentBarcodeText: EditText,
    val recyclerView: RecyclerView,
    private val mContacts:  MutableList<BarcodeList>) : View.OnKeyListener {

init{
    oldQty = try {
        oldQty.toString().toInt()
    } catch (e: NumberFormatException) {
        1
    }
}

override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {

    if (event.action == KeyEvent.ACTION_UP) {

        return if (keyCode == 66) {

            var newQty = 0
            var barcode = ""


            if(QtyModal.text.length >= 12)
            {
                val pattern = Pattern.compile("([0-9]+)?([a-zA-Z]{3}[0-9]+)")
                val matcher = pattern.matcher(QtyModal.text.toString())

                if (matcher.find()) {
                    val qty = if (matcher.group(1)!= null)  matcher.group(1) else QtyModal.text.toString()
                    barcode = if (matcher.group(2)!= null) matcher.group(2) else ""
                    newQty = QtyTextToEdit.text.toString().toInt() + qty.toString().toInt()
                }
            } else
            {
                newQty = QtyModal.text.toString().toInt() + QtyTextToEdit.text.toString().toInt()
                barcode = QtyModal.text.toString()
            }

            QtyTextToEdit.setText(newQty.toString())

// from this line where the code is not working
            recyclerView.postDelayed({
                // Call smooth scroll

 // FYI: I already debug mContacts.size return correct position
 recyclerView.layoutManager.scrollToPosition(mContacts.size)
            }, 100)

 // FYI: current barcode and recyclerview not in modal dialog but in the fragment.
            currentBarcodeText.postDelayed({

                    currentBarcodeText.requestFocus()
                    currentBarcodeText.setText("")

                    if(barcode.length==12)
                    {
                        currentBarcodeText.setText(barcode)
                        currentBarcodeText.setSelection(barcode.length)

                        currentBarcodeText.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
                        currentBarcodeText.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER))
                    }

                }, 130)
 // until this line where the code is not working

            dialog.hide()
            dialog.dismiss()



            true
        } else false
    }

    return false
}

}

Kakashi
  • 3,329
  • 4
  • 26
  • 37

1 Answers1

0

First, let me say that this answer based upon inadequate information in your question (which isn't even formed as a question - see How to ask a good question).

You use a delayed post to update the recycler and you use a delayed post to update a view. If the view (currentbarcode) is set or updated by the delayed post to your recycler, then the results of the delayed post to the current view will be undefined. But, it's impossible to tell based upon your original question.

Your first step should be to remove the delayed post to the currentbarcode and get the automatic scrolling working.

When that is working, you should move your call to update the currentbarcode in to the lambda being passed to the recycler's delayed post.

Les
  • 10,335
  • 4
  • 40
  • 60