我为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);
    }
}

  ask by Anton Kogan translate from so

本文未有回复,本站智能推荐: