提问者:小点点

如何在静态编程语言中获得焦点更改的回收器视图中最后单击项目的位置


我有一个问题,我需要在静态编程语言的回收器视图中获得最后点击的项目位置。

例如:如果我的Recycler视图包含4个编辑文本项。我在第2个位置输入了一个值。当我单击最后一个位置时,我需要获取第2个项目的位置。但现在的问题是,当我单击最后一个位置,即第4个位置时,我仅在焦点更改时获得该位置。请找到我的以下代码。

itemBinding.root.itemCount.setOnFocusChangeListener {
  view,
  hasfocus ->
  if (hasfocus) {
    if (hasfocus && isTouched && (selectedPosition != position)) {
      isTouched = false
      itemBinding.scan.performClick()
    }
  }
  if (!ignoreUpdate && isFrom == "countScreen") {
    updateCountItem(itemResponse, selectedPosition, false)
  }
}

itemBinding.root.itemCount.setOnTouchListener {
  view,
  motionEvent ->
  mRecyclerView!!.post {
    selectedPosition = adapterPosition
    listener.onFocusChange(layoutPosition, "countScreen")
  }
  false
}

private fun updateCountItem(itemResponse: ScanItemModel, position: Int, isCount: Boolean) {
  ignoreUpdate = true
  selectedPosition = position
  itemBinding.root.itemCount.selectAll()
  isTouched = true

  if (!itemDetailBinding.root.itemCount.text.toString().equals("")) {
    var a = 0
    var b = 0
    if (itemBinding.itemCount.text.toString().equals("")) {
      a = 0
    } else {
      a = itemBinding.itemCount.text.toString().toInt()
    }
    if (itemResponse.total != null) {
      if (itemResponse.total!! >= 0) {
        b = itemResponse.total!!
      } else {
        b = 0
      }
    }
    val sum = a.plus(b)
    if (sum < 3) {
      if (itemResponse.total != null) {
        itemResponse.total = sum
      } else {
        itemResponse.total = sum
      }

      itemBinding.root.totalCount.setText(itemResponse.total.toString())
      isOnBind = true
      listener.quantityChanged(
        position,
        itemResponse
      )
      isOnBind = false

      itemBinding.root.itemCount.text.clear()
      ignoreUpdate = false
    } else {
      if (itemResponse.total != null) {
        itemResponse.total = sum
      } else {
        itemResponse.total = sum
      }

      itemBinding.root.itemCount.setText(scanItemResponse.total.toString())
      isOnBind = true
      listener.countQuantityChanged(
        position,
        itemResponse
      )
      isOnBind = false

      itemBinding.root.itemCount.text.clear()
      ignoreUpdate = false
    }
  } else {
    ignoreUpdate = false
  }
}

共1个答案

匿名用户

class RvAdapter: RecyclerView.Adapter....... {
  var itemClicked: Int ? = -1

  fun onItemClikcedPosition(pos: Int) {

    when(pos) {

      1 -> {
        println("previous clicked item $itemClicked")
        itemClicked = pos
      }

      2 -> {
          println("previous clicked item $itemClicked")
          itemClicked = pos
        }
        .....and so on........
    }
  }
}