Question

I have a horizontal list and each list item has a background fill view that is supposed to be a certain percentage of the height of another view (bottleoverlay) inside the listview.

It all works except I can't find the height of the bottleoverlay view using getHeight() as said in this question. I've tried to use onGlobalLayout listener as suggested but the fill height for all items are the same no matter what values are used when setting layoutparams.height

Is there an easier way to do this, the bottleheightoverlay is the same for each item in the list.

public class ImageAdapter extends BaseAdapter {

// varible declaration... 

// getView that displays the data at the specified position in the data set.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
       ......
       ......
      //increase height of filler
      int bottleHeight =  gridView.findViewById(R.id.bottleoverlay).getHeight();
      FrameLayout backgroundfill = (FrameLayout) gridView.findViewById(R.id.backroundfillbar);

      double percent =  products.get(position).value/ products.get(0).value;
      backgroundfill.getLayoutParams().height = (int) ( (bottleHeight/percent));
Was it helpful?

Solution

i got it working using the globallayout listener this is my in imageAdapter

      if(!heightSet ){

      final LinearLayout tv = (LinearLayout) gridView.findViewById(R.id.bottleover);
       ViewTreeObserver vto = tv.getViewTreeObserver();
      vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

          @Override
          public void onGlobalLayout() {

              bottleheight = tv.getHeight();
              heightSet = true;
              System.out.println("bottle hiegiht " + bottleheight);
              //turn off listenter
              ViewTreeObserver obs = tv.getViewTreeObserver();

              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                  obs.removeOnGlobalLayoutListener(this);
              } else {
                  obs.removeGlobalOnLayoutListener(this);
              }
          }

      });
      }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top