Which: ListView.setOnItemClickListener or TextView.setOnClickListener?

孤街浪徒 提交于 2020-01-17 01:02:13

问题


I have a ListView with a custom adapter that only has a TextView, and I want to set a click listener for it.

Which one I should choose: ListView.setOnItemClickListener outside of the custom adapter, or TextView.setOnClickListener inside the custom adapter's getView()? And why?

Thanks!

L.


回答1:


You should definitely use ListView.setOnItemClickListener

  1. Because when you press the list item it gives feedback that you pressed it (like glow background or something)
  2. You are controlling your data from outside of your list, and therefore you have better vision on the objects you are controlling
  3. More object oriented
  4. In my opinion it's easier
  5. If you have only one view in a list row then why bother setting the click listener on the TextView instead of the row?



回答2:


adding onClickListener to views in the getView() method is using when you have 2 or more views that should have their own onClickListener,so for your is better onItemClickListener



来源:https://stackoverflow.com/questions/20446195/which-listview-setonitemclicklistener-or-textview-setonclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!