我有一些这样的代码:
final int height = 0; //I want to get this height after Predraw view
view.addOnPreDrawListener(new OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
int myHeight = itemLayout.getMeasuredHeight();
height = myHeight; //this line has error
return false;
}
});
问题是我如何从侦听器那里获得价值(尤其是原始类型)。它总是显示
Cannot refer to a non-final variable height inside an inner class defined in a different method
然后我尝试将变量更改为final,它显示错误:
The final local variable height cannot be assigned, since it is defined in an enclosing type
我不知道为什么 API 很难使用。