我必须使用一个OnGlobalLayoutListener
对象,然后删除侦听器,我对以下方法解决的不赞成使用的方法有问题。
protected void onCreate(Bundle savedInstanceState) {
final LinearLayout llTotal = (LinearLayout) findViewById(R.id.mmc_ll);
ViewTreeObserver vto = llTotal.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//
// mycode
//
if (Build.VERSION.SDK_INT<16) {
removeLayoutListenerPre16(llTotal.getViewTreeObserver(),this);
} else {
removeLayoutListenerPost16(llTotal.getViewTreeObserver(), this);
}
}
});
}
super.onCreate(savedInstanceState);
}
@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeGlobalOnLayoutListener(listener);
}
@TargetApi(16)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeOnGlobalLayoutListener(listener);
}
这是对的吗?有更好的处理兼容性的方法吗?
使用API 10在模拟器中运行代码我在LogCat中收到以下警告
04-24 09:30:12.565: I/dalvikvm(471): Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener, referenced from method com.my.project.ActivityHome.removeLayoutListenerPost16
04-24 09:30:12.565: W/dalvikvm(471): VFY: unable to resolve virtual method 2950: Landroid/view/ViewTreeObserver;.removeOnGlobalLayoutListener (Landroid/view/ViewTreeObserver$OnGlobalLayoutListener;)V
我是否可以忽略它们,因为我必须以某种方式进行修复?