public static abstract class ViewHolder<T> extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
public abstract void bindItem(T item);
}
Is there a way to ensure bindItem(T item)
parameter T is exactly T instead of Object? (As Type erasure replaces the method parameter type T with Object since it is unbounded.)
This is how I ended up implementing the abstract class:
static class EventHolder extends ViewHolder {
@Override
public void bindItem(Object item) {
}
}
causing the bindItem
parameter type to be Object
since the raw type of ViewHolder was subclassed.
Should have been
static class EventHolder extends ViewHolder<Event> {
@Override
public void bindItem(Event item) {
}
}
Which forces the correct parameter type.
Sorry. Late night coding = stupid mistakes.
You can narrow down the generic type to whatever is needed, for example:
public static abstract class ViewHolder<T extends SuperItem>
...
}
So anywhere inside this class you will get a compile-time warning if there is an object of non-declared type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.