Reputation: 1842
I have the following code, see below. I want to add a View programmatically to the inflated layout - because the View's size, I want to add to the layout, depends on another View's size which I am receiving in the ViewTree Observer (the on pre draw listener of the ViewTree Observer gives you the chance to get the size of a layout item when it uses "fill_parent", therefore it has to be pre drawn to get the size).
How can I do this? LayoutInflater doesn't provide a method for adding a View. Maybe I only need a hint, I know how to create Views programmatically - but how do I add them in such case?
PS: The getView method is used for a ListView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_ITEM0:
convertView = mInflater.inflate(R.layout.event_details_headline, null);
TextView toptext = (TextView) convertView.findViewById(R.id.toptext);
toptext.setText(mData.get(0).getTitle());
final ImageView imageView = (ImageView) convertView.findViewById(R.id.flyer);
AwesomeActivity.imageLoader.DisplayRoundedImage(mData.get(0).getFlyerURL(), imageView);
final int[] flyerheight = {0};
final int[] flyerwidth = {0};
final ImageView border = (ImageView) convertView.findViewById(R.id.border);
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
flyerheight[0] = imageView.getMeasuredHeight();
flyerwidth[0] = imageView.getMeasuredWidth();
// Here I want to add an ImageView based on flyerWidth and flyerHeight
return true;
}
});
break;
}
Upvotes: 0
Views: 2027
Reputation: 31779
You need to find the outer most class or viewgroup used in 'event_details_headline.xml' file and typecast it to a variable. Views can only be added to ViewGroups or its subclasses. Suppose the linearlayout is the outermost view group in ur xml, ur inflate code should look like
LinearLayout layout = (LinearLayout)mInflater.inflate(
R.layout.event_details_headline, null);
layout.addView(YOUR VIEW);
convertView = layout;
This should work.
Upvotes: 2