Android 如何在Kotlin中获取上次单击的项目在recycler视图中的位置(焦点已更改)
我有一个问题,我需要在Kotlin的“回收者”视图中获取上次单击的项目在焦点更改上的位置 例如:如果“我的回收器”视图包含4个编辑文本项。我在第二个位置输入了一个值。当我点击最后一个位置时,我需要得到第二个项目的位置。但现在的问题是,当我点击最后一个位置,也就是第四个位置时,我得到的只是焦点改变的位置。请找到我下面的代码Android 如何在Kotlin中获取上次单击的项目在recycler视图中的位置(焦点已更改),android,kotlin,android-recyclerview,onfocuschangelistener,Android,Kotlin,Android Recyclerview,Onfocuschangelistener,我有一个问题,我需要在Kotlin的“回收者”视图中获取上次单击的项目在焦点更改上的位置 例如:如果“我的回收器”视图包含4个编辑文本项。我在第二个位置输入了一个值。当我点击最后一个位置时,我需要得到第二个项目的位置。但现在的问题是,当我点击最后一个位置,也就是第四个位置时,我得到的只是焦点改变的位置。请找到我下面的代码 itemBinding.root.itemCount.setOnFocusChangeListener { view, hasfocus ->
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
}
}
itemBinding.root.itemCount.setOnFocusChangeListener{view,hasfocus->
如果(hasfocus){
if(hasfocus&&isTouched&&(selectedPosition!=位置)){
isTouched=false
itemBinding.scan.performClick()
}
}
如果(!ignoreUpdate&&isFrom==“countScreen”){
updateCountItem(itemResponse,selectedPosition,false)
}
}
itemBinding.root.itemCount.setOnTouchListener{view,motionEvent->
mRecyclerView!!.post{
selectedPosition=适配器位置
listener.onFocusChange(布局位置,“计数屏幕”)
}
假的
}
private fun updateCountItem(itemResponse:ScanItemModel,position:Int,isCount:Boolean){
ignoreUpdate=true
selectedPosition=位置
itemBinding.root.itemCount.selectAll()
isTouched=真
如果(!itemDetailBinding.root.itemCount.text.toString()等于(“”){
变量a=0
var b=0
if(itemBinding.itemCount.text.toString().equals(“”){
a=0
}否则{
a=itemBinding.itemCount.text.toString().toInt()
}
if(itemResponse.total!=null){
if(itemResponse.total!!>=0){
b=项目响应。总计!!
}否则{
b=0
}
}
val sum=a加(b)
如果(总和<3){
if(itemResponse.total!=null){
itemResponse.total=总和
}否则{
itemResponse.total=总和
}
itemBinding.root.totalCount.setText(itemResponse.total.toString())
isOnBind=true
listener.quantityChanged(
立场,,
项目响应
)
isOnBind=false
itemBinding.root.itemCount.text.clear()
ignoreUpdate=false
}否则{
if(itemResponse.total!=null){
itemResponse.total=总和
}否则{
itemResponse.total=总和
}
itemBinding.root.itemCount.setText(scanItemResponse.total.toString())
isOnBind=true
listener.countQuantityChanged(
立场,,
项目响应
)
isOnBind=false
itemBinding.root.itemCount.text.clear()
ignoreUpdate=false
}
}否则{
ignoreUpdate=false
}
}
请在这方面帮助我。任何帮助都将不胜感激。提前表示感谢和问候。:) 单击时保存帖子
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 ........
}
}
}