recyclerview smoothscroll不能在对话框上工作

时间:2018-03-17 12:23:51

标签: android android-recyclerview kotlin android-dialog

我的申请流程:

stockerFragment:

  • 使用条形码扫描仪输入条形码

  • 如果之前已经扫描过条形码,则滚动平滑到现有条形码并显示对话框模式以编辑数量。

  • 用户在对话框模式中编辑数量后按Enter键,关闭对话框并滚动平滑到最后位置。 < - 此步骤中发生错误。它只是关闭对话框而不是自动滚动到最后一个位置。

这是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
}

}

1 个答案:

答案 0 :(得分:0)

首先,请允许我说这个答案基于您的问题中的不充分信息(甚至不是形成一个问题 - 请参阅How to ask a good question)。

您使用延迟帖子来更新回收站,并使用延迟帖子来更新视图。如果视图(currentbarcode)由延迟发布到您的回收站设置或更新,则延迟发布到当前视图的结果将是未定义的。但是,根据你原来的问题,我们无法判断。

您的第一步应该是将延迟的帖子移除到currentbarcode并自动滚动工作。

当它工作时,您应该移动您的呼叫以将currentbarcode更新为传递给回收者延迟帖子的lambda。