In Android, there are two methods to respond to the click event of the button inside the listview in the activity

Recently, someone in the communication group asked a question: how to respond to the click event of the button inside the listview in the activity instead of in the adapter?

My initial answer to this question is to define a callback interface in the adapter and implement it in the activity to respond to click events.

After thinking about it after work, I think there are two better ways to implement it: using interface callback and using abstract class callback.

Just to review the differences between interfaces and abstract classes, I wrote two demos:

1. Use interface callback:

Adapter class

Activity class:

2. Use abstract class callback

Adapter class:

Activity class:

The difference between the two methods is that when an abstract class is implemented in an activity, only one member variable can be defined to implement it, which cannot be implemented directly by the activity, because Java does not support multiple inheritance. The interface can be implemented directly by activity or by its member variables.

Original link: http://blog.csdn.net/u011895534/article/details/50439547

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>