您无需扩展 AdapterView 即可从自定义视图中的适配器生成视图。您可以扩展LinearLayout
和处理适配器。最简单的解决方案如下所示:
public class CustomAdapterView extends LinearLayout {
private Adapter adapter;
private final DataSetObserver observer = new DataSetObserver() {
@Override
public void onChanged() {
refreshViewsFromAdapter();
}
@Override
public void onInvalidated() {
removeAllViews();
}
};
public CustomAdapterView(Context context) {
super(context);
}
public CustomAdapterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAdapterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Adapter getAdapter() {
return adapter;
}
public void setAdapter(Adapter adapter) {
if (this.adapter != null) {
this.adapter.unregisterDataSetObserver(observer);
}
this.adapter = adapter;
if (this.adapter != null) {
this.adapter.registerDataSetObserver(observer);
}
initViewsFromAdapter();
}
protected void initViewsFromAdapter() {
removeAllViews();
if (adapter != null) {
for (int i = 0; i < adapter.getCount(); i++) {
addView(adapter.getView(i, null, this), i);
}
}
}
protected void refreshViewsFromAdapter() {
int childCount = getChildCount();
int adapterSize = adapter.getCount();
int reuseCount = Math.min(childCount, adapterSize);
for (int i = 0; i < reuseCount; i++) {
adapter.getView(i, getChildAt(i), this);
}
if (childCount < adapterSize) {
for (int i = childCount; i < adapterSize; i++) {
addView(adapter.getView(i, null, this), i);
}
} else if (childCount > adapterSize) {
removeViews(adapterSize, childCount);
}
}
}
由于上面的代码只是一个简单的例子,它没有处理适配器返回不同类型视图的情况(例如Adapter#getViewTypeCount()
返回大于 1 的数字)。
当然,所有为添加/删除视图定义的 LinearLayout 方法都是可用的,因此它们可能会与您的适配器处理发生冲突。您可以通过抛出 UnsupportedOperationException 来禁用它们:
@Override
public void addView(View child) {
throw new UnsupportedOperationException(
"You cannot add views directly without adapter!");
}
(对于所有其他添加/删除方法等等),或者通过覆盖它们来操作适配器的支持数据集(应该强制实现您的自定义接口,允许在适配器接口旁边进行此类修改)。在这两种情况下,请记住在代码中从超类调用 add remove 方法来处理适配器。
编辑:以及扩展 LinearLayout 并支持适配器的 viewTypesCount 的简单实现:
class CustomAdapterViewTypedImpl extends LinearLayout {
private Adapter adapter;
private SparseArray<List<View>> typedViewsCache = new SparseArray<List<View>>();
private final DataSetObserver observer = new DataSetObserver() {
@Override
public void onChanged() {
refreshViewsFromAdapter();
}
@Override
public void onInvalidated() {
removeAllViews();
}
};
public CustomAdapterViewTypedImpl(Context context) {
super(context);
}
public CustomAdapterViewTypedImpl(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAdapterViewTypedImpl(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Adapter getAdapter() {
return adapter;
}
public void setAdapter(Adapter adapter) {
if (this.adapter != null) {
this.adapter.unregisterDataSetObserver(observer);
}
this.adapter = adapter;
if (this.adapter != null) {
this.adapter.registerDataSetObserver(observer);
}
initViewsFromAdapter();
}
protected void initViewsFromAdapter() {
typedViewsCache.clear();
removeAllViews();
View view;
if (adapter != null) {
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, null, this);
addToTypesMap(adapter.getItemViewType(i), view, typedViewsCache);
addView(view, i);
}
}
}
protected void refreshViewsFromAdapter() {
SparseArray<List<View>> typedViewsCacheCopy = typedViewsCache;
typedViewsCache = new SparseArray<List<View>>();
removeAllViews();
View convertView;
int type;
for (int i = 0; i < adapter.getCount(); i++) {
type = adapter.getItemViewType(i);
convertView = shiftCachedViewOfType(type, typedViewsCacheCopy);
convertView = adapter.getView(i, convertView, this);
addToTypesMap(type, convertView, typedViewsCache);
addView(convertView, i);
}
}
private static void addToTypesMap(int type, View view, SparseArray<List<View>> typedViewsCache) {
List<View> singleTypeViews = typedViewsCache.get(type);
if(singleTypeViews == null) {
singleTypeViews = new ArrayList<View>();
typedViewsCache.put(type, singleTypeViews);
}
singleTypeViews.add(view);
}
private static View shiftCachedViewOfType(int type, SparseArray<List<View>> typedViewsCache) {
List<View> singleTypeViews = typedViewsCache.get(type);
if(singleTypeViews != null) {
if(singleTypeViews.size() > 0) {
return singleTypeViews.remove(0);
}
}
return null;
}
}