Android 测量错误的尺寸
我为ExpandableListView创建了几个自定义视图。现在,我需要将可展开列表视图放在scrollview中 ExpandableListView有自己的滚动,我得到的窗口比我需要的小(折叠的ExpandableListView的大小)。所以我决定使用Android 测量错误的尺寸,android,scrollview,expandablelistview,onmeasure,Android,Scrollview,Expandablelistview,Onmeasure,我为ExpandableListView创建了几个自定义视图。现在,我需要将可展开列表视图放在scrollview中 ExpandableListView有自己的滚动,我得到的窗口比我需要的小(折叠的ExpandableListView的大小)。所以我决定使用onMeasure方法来设置扩展列表的位置。现在一切都正常了,但我在大设备和小设备上的可扩展列表视图之间有空间,一个列表在另一个列表上 现在的问题是:为什么我得到了错误的维度 public class FoodList extends Li
onMeasure
方法来设置扩展列表的位置。现在一切都正常了,但我在大设备和小设备上的可扩展列表视图之间有空间,一个列表在另一个列表上
现在的问题是:为什么我得到了错误的维度
public class FoodList extends LinearLayout {
public final static int BraceletStandart = 0;
public final static int BraceletPro = 1;
public int viewHeight;
ArrayList <ChildFoodSet> childFoodSets = new ArrayList<>();
FoodFooter footer;
HeaderFoodSet hfs;
FoodHeaderRow headerRow;
FoodWidgetKeeper listKeeper;
Context context;
View headerView;
View footerView;
View childView;
int headerMeasuredHeight;
int footer1MeasuredHeight;
int childMeasuredHeight;
int bracerOption;
ExpandableListView expandableListView;
public FoodList(Context context ) {
super(context);
init();
}
public FoodList(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FoodList(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.food_list, this);
this.expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
context=getContext();
}
private void fillData () {
listKeeper= new FoodWidgetKeeper(bracerOption);
listKeeper.addHeader(headerRow);
footer = new FoodFooter(context,expandableListView,hfs);
listKeeper.addfooter(footer);
expandableListView.setGroupIndicator(null);
expandableListView.addFooterView(footer);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
listKeeper.headers.get(groupPosition).hfs.changeExpandCollapse(footer);
return false;
}
});
FoodWidgetExListAdapter adapter = new FoodWidgetExListAdapter(context, listKeeper);
adapter.countHeaders();
expandableListView.setAdapter(adapter);
Activity activity = (Activity) context;
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
headerView =vi.inflate(R.layout.food_widget_list_header, null);
childView =vi.inflate( R.layout.food_widget_child_row, null );
footerView =vi.inflate( R.layout.food_widget_footer, null );
TextView txtRightMidle = (TextView) headerView.findViewById(R.id.text_right_botn_header_food_num);
TextView txtRightMidle2 = (TextView) headerView.findViewById(R.id.text_right_botn_header_food_rsk);
FoodNutritionNum nutritionNum = (FoodNutritionNum) headerView.findViewById(R.id.header_nutrition_numb_row);
TextView txtChild6 = (TextView) childView.findViewById(R.id.food_child_3row_right);
TextView txtChild5 = (TextView) childView.findViewById(R.id.food_child_3row_left);
FoodNutritionNum foodChildNutritionNum =(FoodNutritionNum) childView.findViewById(R.id.child_nutrition_numb_row);
headerView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
headerMeasuredHeight = headerView.getMeasuredHeight();
footerView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
footer1MeasuredHeight = footerView.getMeasuredHeight();
childView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
childMeasuredHeight = childView.getMeasuredHeight();
}
private void setGroupRow(HeaderFoodSet hfs){
headerRow = new FoodHeaderRow(context, hfs);
this.hfs=hfs;
}
private void addchildRow(ChildFoodSet foodset){
headerRow.addChildRow(new FoodChildRow(context,foodset));
childFoodSets.add(foodset);
}
public void addGroupAndChilds(HeaderFoodSet hfs,ArrayList <ChildFoodSet> childFoodSets, int bracerOption){
setGroupRow(hfs);
for (int i=0;i<childFoodSets.size();i++){
addchildRow(childFoodSets.get(i));
}
this.bracerOption=bracerOption;
fillData();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = getMeasuredWidth();
viewHeight = getMeasuredHeight();
if (hfs.isExpanded) viewHeight = headerMeasuredHeight+footer1MeasuredHeight+ childMeasuredHeight*childFoodSets.size()-1;
else viewHeight = headerMeasuredHeight+footer1MeasuredHeight;
Log.i("Onmeasure", " header " + headerMeasuredHeight + " footer " + footer1MeasuredHeight + " child " + childMeasuredHeight + " all: " + viewHeight);
setMeasuredDimension(width, viewHeight);
}
}
公共类食物列表扩展了LinearLayout{
公共最终静态int标准件=0;
公共最终静态int=1;
公众视野高度;
ArrayList childFoodSets=新的ArrayList();
食物足;
HeaderFoodSet hfs;
食物头;
食品保管员;
语境;
查看headerView;
视图页脚视图;
查看儿童视图;
内部水头测量高度;
int footer1测量重量;
儿童身高;
内斜撑;
ExpandableListView ExpandableListView;
公共食物列表(上下文){
超级(上下文);
init();
}
公共食物列表(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共食物列表(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
私有void init(){
充气(getContext(),R.layout.food_list,this);
this.expandableListView=(expandableListView)findViewById(R.id.expandableListView1);
context=getContext();
}
私有void fillData(){
listKeeper=新的FoodWidgetKeeper(括号);
listKeeper.addHeader(headerRow);
页脚=新的FoodFooter(上下文、可扩展列表视图、hfs);
listKeeper.addfooter(页脚);
expandableListView.setGroupIndicator(空);
expandableListView.addFooterView(页脚);
expandableListView.setOnGroupClickListener(新的expandableListView.OnGroupClickListener(){
@凌驾
公共布尔onGroupClick(ExpandableListView父视图、视图v、int-groupPosition、长id){
listKeeper.headers.get(groupPosition).hfs.changeExpandCollapse(footer);
返回false;
}
});
FoodWidgetExListAdapter=新的FoodWidgetExListAdapter(上下文,listKeeper);
adapter.countHeaders();
expandableListView.setAdapter(适配器);
活动=(活动)上下文;
LayoutInflater vi=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
headerView=vi.inflate(R.layout.food\u widget\u list\u header,null);
childView=vi.充气(R.layout.food\u widget\u child\u row,空);
footerView=vi.inflate(R.layout.food\u widget\u footer,null);
TextView txtRightMidle=(TextView)headerView.findViewById(R.id.text\u right\u botn\u header\u food\u num);
TextView txtRightMidle2=(TextView)headerView.findViewById(R.id.text\u right\u botn\u header\u food\u rsk);
FoodNutritionNum nutritionNum=(FoodNutritionNum)headerView.findViewById(R.id.header\u nutrition\u numb\u row);
TextView txtChild6=(TextView)childView.findViewById(R.id.food\u child\u 3row\u right);
TextView txtChild5=(TextView)childView.findViewById(R.id.food\u child\u 3row\u left);
FoodNutritionNum foodChildNutritionNum=(FoodNutritionNum)childView.findViewById(R.id.child\u nutrition\u numb\u row);
headerView.度量(度量未指定,度量未指定);
headerMeasuredHeight=headerView.getMeasuredHeight();
footerView.measure(未指定度量值,未指定度量值);
footer1MeasuredHeight=footerView.getMeasuredHeight();
childView.measure(MeasureSpec.unspected,MeasureSpec.unspected);
childMeasuredHeight=childView.getMeasuredHeight();
}
私有void setGroupRow(HeaderFoodSet hfs){
headerRow=新食品headerRow(上下文,hfs);
这个.hfs=hfs;
}
私有void addchildRow(ChildFoodSet foodset){
headerRow.addChildRow(新食品ChildRow(上下文,食品集));
添加(foodset);
}
public void addGroupAndChilds(HeaderFoodSet hfs、ArrayList childfoodset、int-braseroption){
setGroupRow(hfs);
对于(int i=0;i