我的代码中有一堆动画侦听器,其中大部分时间界面中只有一个方法使用。
所以我创建了这个包装器:
public class AnimationWrapper {
private Animation mAnimation;
public AnimationWrapper(Animation animation) {
mAnimation = animation;
}
public Animation getAnimation() {
return mAnimation;
}
public interface OnAnimationEnd {
void onAnimationEnd(Animation animation);
}
public interface OnAnimationStart {
void onAnimationStart(Animation animation);
}
public void setAnimationEndListener(OnAnimationEnd listener) {
mAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
listener.onAnimationEnd(animation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public void setAnimationStartListener(OnAnimationStart listener) {
mAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
listener.onAnimationStart(animation);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
那样的代码如下:
Animation animation = AnimationUtils.loadAnimation(context, R.anim.my_animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
doSomething();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
看起来像这样:
AnimationWrapper wrapper = new AnimationWrapper(AnimationUtils.loadAnimation(context, R.anim.my_animation));
wrapper.setAnimationEndListener(a -> doSomething());
我只是想知道是否有更好的方法可以将非功能性接口拆分为一个方法的单独接口,这样每个方法都可以与lambda表达式分开使用。
答案 0 :(得分:4)
我会将功能性的单方法侦听器包装成“真正的”多方法侦听器(即应用适配器设计模式),而不是包装动画:
public interface OnAnimationEnd {
void onAnimationEnd(Animation animation);
}
public interface OnAnimationStart {
void onAnimationStart(Animation animation);
}
public class AnimationListeners {
public static Animation.AnimationListener adapt(OnAnimationStart oas) {
return new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
oas.onAnimationStart(animation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
// same for the end
}
你只需要使用
animation.setAnimationListener(AnimationListeners.adapt(a -> doSomething()));
使用adjust方法的静态导入,它变为
animation.setAnimationListener(adapt(a -> doSomething()));
答案 1 :(得分:2)
我建议应用构建器模式构建[Thu Mar 09 09:42:19.791559 2017] [ssl:warn] [pid 1453] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]
[Thu Mar 09 09:42:19.905721 2017] [mpm_prefork:notice] [pid 1453] AH00163: Apache/2.4.10 (Linux/SUSE) OpenSSL/1.0.1i-fips PHP/5.6.30 configured -- resuming normal operations
[Thu Mar 09 09:42:19.905770 2017] [core:notice] [pid 1453] AH00094: Command line: '/usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D SYSTEMD -D FOREGROUND'
[Thu Mar 09 19:52:23.863004 2017] [ssl:warn] [pid 1318] AH01882: Init: this version of mod_ssl was compiled against a newer library (OpenSSL 1.0.1k 8 Jan 2015, version currently loaded is OpenSSL 1.0.1i-fips 6 Aug 2014) - may result in undefined or erroneous behavior AH00557: httpd2-prefork: apr_sockaddr_info_get() failed for linux-kny7 AH00558: httpd2-prefork: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[Thu Mar 09 19:52:33.915213 2017] [ssl:warn] [pid 1318] AH01882: Init: this version of mod_ssl was compiled against a newer library (OpenSSL 1.0.1k 8 Jan 2015, version currently loaded is OpenSSL 1.0.1i-fips 6 Aug 2014) - may result in undefined or erroneous behavior
[Thu Mar 09 19:52:33.915299 2017] [ssl:warn] [pid 1318] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]
[Thu Mar 09 19:52:35.175637 2017] [mpm_prefork:notice] [pid 1318] AH00163: Apache/2.4.10 (Linux/SUSE) OpenSSL/1.0.1i-fips PHP/5.6.30 configured -- resuming normal operations [Thu Mar 09 19:52:35.175813 2017] [core:notice] [pid 1318] AH00094: Command line: '/usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D
实例,使用方便的方法获取AnimationListener
s,将它们保存到字段中,并在最终的Consumer<Application>
阶段使用这些值。
由于build
已经存在(并且您可能写的全部是三个新Consumer
个副本),因此无需倍增自己的接口。
我不熟悉Android API,因此我很快就编写了自己的类。这个想法是一样的。
Concumer<Animation>
答案 2 :(得分:0)
结帐AnimatorListenerAdapter
。这是一个抽象类,它通过提供空方法来实现Animation.AnimationListener
。扩展AnimatorListenerAdapter
时,只需实现所需的方法即可。这些适配器是Java事件处理中的常见模式。