What if I have 50 types of views? Should I have 50 static inner classes in my adapter? According to this answer, yes.
My first thought was to move each viewholder inner class into a seperate public class, but they have to be static. So encapsulate each one into a public class, to make the inner class static? Are there any nicer alternatives?
edit: code sample: So this would be a good solution? Doesn't this also kill performance?
public class MainViewHolder extends DragSortAdapter.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
View container;
TextView title;
//called in onCreateViewHolder in adapter
public MainViewHolder(DragSortAdapter adapter, View itemView) {
super(adapter, itemView);
container = itemView.findViewById(R.id.card_root);
title = container.findViewById(R.id.text);
}
//called by onBindViewHolder in adapter
public void setData(Data data) {
title.setText(data.title);
}
}
edit2: sample, for when a new instance is returned of the viewholder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new MainViewHolder(...);
case 2: return new MainViewHolderOther(...);
...
}
}