Android 获取listitem内layoutview的高度
我有一个Android 获取listitem内layoutview的高度,android,view,horizontallist,Android,View,Horizontallist,我有一个水平列表,每个列表项都有一个背景填充视图 应该是listview中另一个视图(覆盖)高度的一定百分比 除了使用getHeight() 如中所述。 我已尝试按照建议使用onGlobalLayout listener,但无论在设置layoutparams.height时使用什么值,所有项目的填充高度都是相同的 如果有更简单的方法,列表中的每个项目的瓶高覆盖都是相同的 public class ImageAdapter extends BaseAdapter { // varible dec
水平列表
,每个列表项都有一个背景填充视图
应该是listview中另一个视图(覆盖)高度的一定百分比
除了使用getHeight()
如中所述。
我已尝试按照建议使用onGlobalLayout listener,但无论在设置layoutparams.height时使用什么值,所有项目的填充高度都是相同的
如果有更简单的方法,列表中的每个项目的瓶高覆盖都是相同的
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));
我用globallayout监听器让它工作了
这是我的图像适配器
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);
}
}
});
}