Reputation: 7233
In my common library code I am trying to create a inflater helper to manage multiple views in a ListView. This code is intended to be used in many projects.
I would like to write unit test for the following code for that I need some xml layout to be created. I know it is possible to have resources in Android library but I want that layout resource just for testing and not to be shipped along with it. Below is the code that I want to test.
public abstract class ListViewItemHolder {
protected View itemView;
public View getItemView() {
return itemView;
}
public void initialize(View itemView) {
this.itemView = itemView;
copySubviews();
}
protected abstract void copySubviews();
}
public class ListViewItemInflater {
private LayoutInflater inflater;
private ArrayList<Pair<Integer, Class<ListViewItemHolder>>> viewHolders;
public ListViewItemInflater(Context context) {
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.viewHolders = new ArrayList<Pair<Integer, Class<ListViewItemHolder>>>();
}
public <T extends ListViewItemHolder> void registerViewHolder(int resourceId, Class<T> viewHolderClass) {
viewHolders.add(new Pair<Integer, Class<ListViewItemHolder>>(resourceId, (Class) viewHolderClass));
}
public <T extends ListViewItemHolder> T getViewHolder(View itemView, int index) {
if (itemView == null) {
Pair<Integer, Class<ListViewItemHolder>> viewInfo = viewHolders.get(index);
itemView = inflater.inflate(viewInfo.first, null);
try {
T viewHolder = (T) viewInfo.second.newInstance();
viewHolder.initialize(itemView);
itemView.setTag(viewHolder);
}
catch (Exception e) { /* TODO: Still to decide what to do here. */ }
}
return (T) itemView.getTag();
}
}
ListViewItemInflater registers multiple layout ids and view holder classes and on demand inflates the view for list item and populates the view holder class. I have tried to automate the view holder patterns for list item handling.
Upvotes: 1
Views: 338
Reputation: 20211
I think you can use shrinkResources
for this:
http://tools.android.com/tech-docs/new-build-system/resource-shrinking
Upvotes: 1