(科特琳)尝试单击某个项目时,RecyclerView的位置返回-1

时间:2019-07-16 14:51:50

标签: android kotlin android-recyclerview onclicklistener

我是Android开发(和Kotlin)的新手。 我正在尝试实现RecyclerView(工作正常),当我单击特定的行时,它将打开一个新的活动(意图)。 但是,每当我按下/单击行之一时,我只能获得返回的值“ -1”。

我尝试了许多不同的方法(您应该在浏览器中看到选项卡的数量)。 对于像RecyclerView这样常见的事物,这似乎应该是相当简单的事情,但是由于任何原因,我都无法使其正常工作。

这是我的RecyclerView适配器文件:

class PNHLePlayerAdapter (val players : ArrayList<PNHLePlayer>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {

var onItemClick: ((Int)->Unit) = {}

// Gets the number of items in the list
override fun getItemCount(): Int {
    return players.size
}

// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val itemView = LayoutInflater.from(context).inflate(
        R.layout.pnhle_list_item,
        parent,
        false
    )

    val viewHolder = ViewHolder(itemView)

    itemView.setOnClickListener {
        onItemClick(viewHolder.adapterPosition)
    }

    return ViewHolder(itemView)
}

// Binds each item in the ArrayList to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.tvPlayerName?.text = players[position].Name
    holder.tvPlayerRank?.text = position.toString()
    holder.tvPNHLe?.text = players[position].PNHLe.toString()
    holder.tvTeam?.text = players[position].Team
    holder.ivLeague?.setImageResource(leagueImageID)

}


}

class ViewHolder (view: View) : RecyclerView.ViewHolder(view)  {

val linLayout = view.hor1LinearLayout
val ivTeam = view.teamImageView
val tvPlayerName = view.playerNameTextView
val tvPlayerRank = view.rankNumTextView
val tvPNHLe = view.pnhleTextView
val tvTeam = view.teamTextView
val ivLeague = view.leagueImageView
}

如您所见,有一个类属性“ onItemClick”,它使用lambda作为click回调。 视图膨胀后,我在onCreateViewHolder方法中设置了onOnClickListener。

接下来,在“活动”中,将列表添加到我的适配器中并设置回叫。 但是,每次我“举杯”该位置时,它都显示为“ -1”。

val adapter = PNHLePlayerAdapter(list, this)
    adapter.onItemClick = { position ->
        Toast.makeText(this, position.toString(),Toast.LENGTH_SHORT).show()
        var intent = Intent(this, PlayerCardActivity::class.java)
        //startActivity(intent)   
    }
    rv_player_list.adapter = adapter

也许我没有考虑适当,但是该位置不应该代表RecyclerView中该项目的行号吗??? 理想情况下,我需要使用该位置,以便可以从“列表”(ArrayList)中获取正确的项,以便可以使用Intent将信息传递给下一个Activity

2 个答案:

答案 0 :(得分:2)

我找到了问题。

onCreateViewHolder中更改此行:

return ViewHolder(itemView)

对此:

return viewHolder

答案 1 :(得分:1)

我将像这样重组适配器:

class PNHLePlayerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Adapter.ViewHolder>() {

interface AdapterListener {
    fun onItemSelected(position: Int?)
}

var players: List<Player> = listOf()
    set(value) {
        field = value
        this.notifyDataSetChanged()
    }
var listener: AdapterListener? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context)
        .inflate(R.layout.item_car_selector, parent, false)
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.bind(position)
}

override fun getItemCount(): Int {
    return brands.size
}

inner class ViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
    private var position: Int? = null
    private val baseView: LinearLayout? = view.findViewById(R.id.baseView) as LinearLayout?
    ...

    init {
        baseView?.setOnClickListener {
            listener?.onManufacturerSelected(position)
        }
    }

    fun bind(position: Int) {
        this.position = position

        ...
    }
  }
}

然后从您的活动/片段中将侦听器设置为adapter.listener = this,并实现onItemSelected(position:Int?)

override fun onItemSelected(position: Int?) {
    ...
}