Say I want to make my own event listener for my class, how do I do that? Do I need to maintain a thread manually?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Create an interface that will be implemented by your activity:
Now you need to know when the event is actually occurring. For example when the user touches a point on screen, override onTouchEvent method:
Similarly, you can create a specific event that you want. (examples could be touch down, wait for exactly 2 seconds and release- you would need to do some logic inside touch event).
In your activity, you can use the customView object to set an eventListener as such:
It can be done in the following way
First create an interface class :
Then create the class that controls the interface :
That is all. Let's use it now
I found this tutorial to be VERY HELPFUL. It explains the four steps to using a custom listener to manage callbacks in your code:
1.Define an interface as an event contract with methods that define events and arguments which are relevant event data.
2.Setup a listener member variable and setter in the child object which can be assigned an implementation of the interface.
3.Owner passes in a listener which implements the interface and handles the events from the child object.
4.Trigger events on the defined listener when the object wants to communicate events to it's owner
Hope it helps!