锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

最大高度的listview

时间:2022-08-19 20:30:01 ds30p1传感器ds30nb传感器ds30c1光电传感器

public class DynamicHeightListView extends ListView {    private int maxHeight = BdUtilHelper.getDimens(getContext(), R.dimen.ds400);    public DynamicHeightListView(Context context) {   super(context);  }    public DynamicHeightListView(Context context, AttributeSet attrs) {   super(context, attrs);  }    public DynamicHeightListView(Context context, AttributeSet attrs, int defStyle) {   super(context, attrs, defStyle);  }    @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   super.onMeasure(widthMeasureSpec, heightMeasureSpec);   Log.i("ooo", "onMeasure...");  }    @Override  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {   super.onLayout(changed, left, top, right, bottom);   Log.i("ooo", "onLayout...");   setViewHeightBasedOnChildren();  }    public void setViewHeightBasedOnChildren() {   ListAdapter listAdapter = this.getAdapter();   if (listAdapter == null) {    return;   }   // int h = 10;   // int itemHeight = BdUtilHelper.getDimens(getContext(), R.dimen.ds30);   int sumHeight = 0;   int size = listAdapter.getCount();     for (int i = 0; i < size; i  ) {    View v = listAdapter.getView(i, null, this);    v.measure(0, 0);    Log.i("melon", "item.heiht = "   v.getMeasuredHeight());    sumHeight  = v.getMeasuredHeight();     }     if (sumHeight > maxHeight) {    sumHeight = maxHeight;   }   android.view.ViewGroup.LayoutParams params = this.getLayoutParams();   // this.getLayoutParams();   params.height = sumHeight;     this.setLayoutParams(params);  }    public int getMaxHeight() {   return maxHeight;  }    public void setMaxHeight(int maxHeight) {   this.maxHeight = maxHeight;  }   }

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章