AndroidBaseAdapter.getView回收视图
Android BaseAdapter.getView recycling view
我正在扩展 Android BaseAdapter。
关于方法
getView (int position, View convertView, ViewGroup parent)
文档说
You should check that this view is non-null and of an appropriate type
before using. If it is not possible to convert this view to display
the correct data, this method can create a new view.
我想知道如何检查视图的类型是否合适?
假设我希望有一个带有两个 TextView 子项的 LinearLayout?我该如何正确检查?
这是否正确?
if (convertView instanceof LinearLayout) {
if (convertView.findViewById(someid) != null) {
//its what I expect...
}
}
如果您的适配器中只有一种视图类型,那么您只需要检查它是否为非空。
检查视图类型仅在您有多个视图类型时适用(当您将 getViewTypeCount() 重写为 return 1 以外的任何值时),那么您需要根据位置。基本上,如果您不覆盖 getViewTypeCount() 则只需检查 null.
如果您有多个视图,创建一个 ViewHolder class 是合适的,这可以帮助您在 运行 期间在布局上获取视图,因为您在每个视图中使用一个支架
我正在扩展 Android BaseAdapter。
关于方法
getView (int position, View convertView, ViewGroup parent)
文档说
You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
我想知道如何检查视图的类型是否合适?
假设我希望有一个带有两个 TextView 子项的 LinearLayout?我该如何正确检查?
这是否正确?
if (convertView instanceof LinearLayout) {
if (convertView.findViewById(someid) != null) {
//its what I expect...
}
}
如果您的适配器中只有一种视图类型,那么您只需要检查它是否为非空。
检查视图类型仅在您有多个视图类型时适用(当您将 getViewTypeCount() 重写为 return 1 以外的任何值时),那么您需要根据位置。基本上,如果您不覆盖 getViewTypeCount() 则只需检查 null.
如果您有多个视图,创建一个 ViewHolder class 是合适的,这可以帮助您在 运行 期间在布局上获取视图,因为您在每个视图中使用一个支架