9

嘿,我正在尝试生成这样的类:

public abstract class ResourceListAdapter<T extends ResourceViewHolder> extends RecyclerView.Adapter<T> {}

目前我可以生成:

public abstract class ResourceListAdapter extends RecyclerView.Adapter<?> {}

使用以下代码:

TypeSpec type = TypeSpec.classBuilder(thisClass)
    .superclass(ParameterizedTypeName.get(adapterClassName, WildcardTypeName.subtypeOf(Object.class)))
    .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
    .build();

我也可以做这样的事情:

private ResourceListAdapter<? extends ResourceViewHolder> adapter;

和:

ParameterizedTypeName.get(thisClass,WildcardTypeName.subtypeOf(resourceViewHolderClassName));

但我无法将其结合起来。那你有什么想法吗??

4

1 回答 1

14

我有一个解决方案!

TypeSpec type = TypeSpec.classBuilder(thisClass)
            .superclass(ParameterizedTypeName.get(adapterClassName, TypeVariableName.get("T")))
            .addTypeVariable( TypeVariableName.get("T", resourceViewHolderClassName))

会产生:

public abstract class ResourceListAdapter<T extends ResourceViewHolder> extends RecyclerView.Adapter<T> {}
于 2016-12-09T18:37:05.333 回答