Gau*_*tam 49 android android-context android-adapter
在我在互联网上找到的许多代码示例中,context都是在适配器的构造函数中获得的.
此上下文用于获取方法中inflater的视图膨胀getView.
我的问题是为什么在构造函数中获取上下文时可以轻松获得这样的上下文
LayoutInflater inflater;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater == null){
Context context = parent.getContext();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
...
...
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
也有任何理由不使用上述方法,因为直到现在我还没有遇到任何使用它的问题.
Rid*_*lly 33
在构造函数中获取Context具有(至少)三个优点:
getView()被召唤.parent是null.但是,如果您的解决方案没有任何问题,您可以坚持下去.
| 归档时间: |
|
| 查看次数: |
50635 次 |
| 最近记录: |