我为ExpandableListView创建了一些自定义视图。现在我需要将我的可扩展列表视图放在scrollview中。
ExpandableListView有他自己的滚动,我得到的窗口比我需要的小(折叠的ExpandableListView的大小)。所以我决定使用onMeasure
方法为扩展列表设置位置。现在一切都运行正常,但我在大型设备上的ExpandableListView和小型设备之间有一个空间,一个列表位于另一个列表中。
现在问题是:为什么我的尺码错了?
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);
}
}