Android: Add event listeners to every item in a ListView

后端 未结 2 612
感情败类
感情败类 2020-12-21 05:49

I have an Android app with a ListView, and each row in the list has a TextView and a Button. What I want to do is add an OnClickListener to each Button in the ListView, but

2条回答
  •  渐次进展
    2020-12-21 06:42

    Regarding Cristian's answer, one thing I discovered is that getView is called many times, not just when the view is created. So, you will be executing your getView code more frequently than you might think.

    If the attributes (e.g. the OnClick listener) you are adding are invariant across all elements in the list, you can override newView instead. It will be called exactly once for each displayed row in the ListView. However, be warned that ListView recycles views, so as you scroll, the ones that drop off one end of the view are reused on the other, but with new data from the cursor. Again, as long as your attributes are invariant, this will work great.

提交回复
热议问题