Best way to implement View.OnClickListener in android

后端 未结 14 2293
盖世英雄少女心
盖世英雄少女心 2020-11-27 05:25

Suppose we have an Activity with a lot of views on which OnClickListener is to be registered.

The most common way to implement this is to let the Activi

相关标签:
14条回答
  • 2020-11-27 06:20

    First approach is better than the other because thats why View.OnClickListener is an Interface instead of an abstract class. besides the later might leak in various situations since you are using a non-static inner class.

    0 讨论(0)
  • 2020-11-27 06:20

    A small remark to this, and maybe a little bit of topic.

    What, if we not just implement OnClickListener and we have a bunch of other Listeners / Callback to implement. In my Opinion it will get messy to implement all of these in the class instead of using anonymous classes / lambda. It is hard to remember wich method belongs to which interface.

    So if we have to implement an interface (in this case OnClickListener) multiple times it my be a good solution to implement on class base and use the switch/case.

    But if we have to implement multiple interfaces it may be a good solution to use anonymous classes / lambda

    0 讨论(0)
提交回复
热议问题