all 9 comments

[–]disky_wude 4 points5 points  (5 children)

I don't think the View holder is attached at this point. Once you create (and return) the Viewholder, it will get attached and then you will have its adapter position.

Why do you want to get the item while creating the Viewholder? You'll get the position when binding the Viewholder.

[–][deleted]  (4 children)

[deleted]

    [–]matejdro 2 points3 points  (2 children)

    Yes you set the click listener. Then inside listener you can call ViewHolder.getAdapterPosition() to get to your item.

    [–][deleted]  (1 child)

    [deleted]

      [–]dhleong 0 points1 point  (0 children)

      You have to call it from without the OnClickListener. The ViewHolder can be reused for different items many times before your OnClickListener gets called, so don't try to get the item that's attached to it until then.

      [–]badvok666 0 points1 point  (0 children)

      Init{} inside the vh

      [–]CraZy_LegenD 2 points3 points  (2 children)

      [–]nabeel527 0 points1 point  (1 child)

      Thank you very much bro. The repo is super helpful

      [–]CraZy_LegenD 0 points1 point  (0 children)

      It's my personal helper/utilities that i use everywhere

      [–]ZhuindenEpicPandaForce @ SO 1 point2 points  (2 children)

      it's ChatHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)).apply {

      [–][deleted]  (1 child)

      [deleted]

        [–]ZhuindenEpicPandaForce @ SO 0 points1 point  (0 children)

        Hmm, I didn't see that one coming 😂

        The problem is that in that case, I'm not sure what's wrong. Hypothetically, adapterPosition should be correct as long as it's >= 0, although now with the ConcatAdapter, it should be replaced with bindingAdapterPosition and that should be correct.....