RecyclerView wrong position set onBindViewHolder

前端 未结 4 1311
误落风尘
误落风尘 2021-02-18 13:30

At First, my RecyclerView items are ok, but on scrolling, items are shown on the wrong position for example: item 6 shown in position 67. Although onClick listener and getAdapte

4条回答
  •  一整个雨季
    2021-02-18 13:59

    In my case this problem was happening because I had setHasStableIds(true) in one RecyclerView.Adapter (in fact, it was HasStableIds = true; since I work in Xamarin.Android).

    After removing that line (HasStableIds is false by default), all positions got back to normal and I didn't need to disable recycling or to return position from GetItemViewType(int position) (which is nice because I actually use this method to choose between multiple ViewHolder types). Then just out of interest I tried to enable HasStableIds and override long GetItemId(int position) – it also worked well.

    So my conclusion is: if for some reason you need to enable HasStableIds, you should override both long GetItemId(int position) and int GetItemViewType(int position) in your RecyclerView.Adapter's subclass. If you do not have unique item IDs and/or different view types, just returning back the position parameter will do the job.

提交回复
热议问题