Search code examples
androidlistviewonclickandroid-arrayadaptergetview

Setting onClick in getView() vs setting onListItemClick() on listview


Setting onClick in adapter's getView() and setting onListItemClick() on the listview can both achieve the same result. However, I heard that the first method is a bad practice. Can someone explain why? Is there an performance difference the two?


Solution

  • When you are adding an OnItemClickListener on the ListView, the ListView is actually adding a simple OnClickListener to all the view added by the adapter, with easy access to the view position.

    You could do it yourself from the adapter but you would have to take care of view recycling by yourself.

    If you are doing it correctly from the adapter, there is no performance difference.

    Be aware that if you are using RecyclerView, there is no OnItemClickListener(), you will have to do it from the adapter anyway.