17. @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec);
tinyGap = widthSpecSize/100;
myComponent.measure(View.MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));
// more component’s measuring goes there
setMeasuredDimension(widthSpecSize, newHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
myComponent.layout(x, y, x+myComponent.getMeasuredWidth(), y+titleLabel.getMeasuredHeight());
// more component’s layout goes there
}
37. ACTIONBAR BEHAVIOR
Touch here shows
a menu with options
Touch here moves up in hierarchy
Touch here performs action
Touch here show a menu with options
Any extension should stay consistent with this
58. interface ViewAdapterBinder<T, V> {
public void bindViewToData(V view, T data);
}
public class ArrayViewGroupAdapter<T, V extends ViewGroup> extends ArrayAdapter<T>
BIND DATATOVIEW
59. public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
V v = null;
try {
v = (V) convertView;
} catch(ClassCastException ccex) {}
// safe to ignore, keep null to force new instance to be created
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
v = getInstanceOfV();
}
T data = getItem(position);
if (data != null && binder != null) {
binder.bindViewToData(v, data);
} else {
// signal error here
prn.log("Can't bind data to view " + position);
}
// the view must be returned to our activity
return v;
}
60. private V getInstanceOfV() {
ParameterizedType superClass = (ParameterizedType)
getClass().getGenericSuperclass();
Class<V> type = (Class<V>) superClass.getActualTypeArguments()[1];
try {
return type.getDeclaredConstructor(Context.class).newInstance(getContext());
} catch (Exception ex) {
// Oops, no default constructor
throw new RuntimeException(ex);
}
}
61. public class SampleArrayAdapter extends ArrayViewGroupAdapter<SampleData, SampleListItem> {
public SampleArrayAdapter(Context context) {
super(context, new ArrayViewGroupAdapter.ViewAdapterBinder<SampleData, SampleListItem>() {
@Override
public void bindViewToData(SampleListItem view, SampleData data) {
view.setTitle(data.getTitle());
view.setDetails(data.getDetails());
}
});
}
}