getChildView not being called

前端 未结 3 773
余生分开走
余生分开走 2021-01-02 08:06

I am making a menu which includes a custom ExpandableListView adapter. Despite trying to match my code as close to the API examples and any other examples I\'ve seen online

3条回答
  •  情歌与酒
    2021-01-02 08:14

    Bummer that no one was able to answer this...but, luckly I was able find a solution for this problem so others out there can add expandable list views to their fragments too.

    I found the solution while checking out the source code for the spinner (because I wanted to make a spinner with custom entries). The key point in the code was in it's "onMeasure" when it measures all of it's children...well, up to a pre-defined limit of 15, but measuring the children is the main point.

    = Solution = When creating your adapter, you need to pass in your parent view. This is key because it's the only link you have to the view from the adapter (only easy way).

    public Context context;
    public ExpandableListView parent;
    private LayoutInflater inflator;
    // Only measure this many items to get a decent max height.
    private static final int MAX_ITEMS_MEASURED = 15;
    
    public ExpandableFaceList(Context context, final ExpandableListView parent)
    {
        this.context = context;
        this.parent = parent;
        this.inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        mDensity = context.getResources().getDisplayMetrics().density;
    }
    

    Next, in your "getGroupView", because that seems to be the only thing which is commonly called, you'll need to add the following code:

    public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent)
    {
        if(convertView != null)
        {
            int nH = 0;
            if(isExpanded)
            {
                nH = measureChildrenHeight(groupPosition);
            } else
            {
                nH = convertView.getMeasuredHeight();
            }
            parent.getLayoutParams().height = (int) (nH * mDensity);
    
            parent.invalidate();
        }
    
        return convertView;
    }
    

    The key part in the above code is the "measureChildrenHeight", the "convertView.getMeasuredHeight()" is just to restore the ELV when collapsed. The measure children code is as follows:

    int measureChildrenHeight(int groupPosition)
    {
        int height = 0;
        View itemView = null;
        LinearLayout viewGroup = new LinearLayout(context);
        viewGroup.setOrientation(LinearLayout.VERTICAL);
    
        final int widthMeasureSpec =
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        final int heightMeasureSpec =
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    
        // Make sure the number of items we'll measure is capped. If it's a huge data set
        // with wildly varying sizes, oh well.
        int start = 0;//Math.max(0, getSelectedItemPosition());
    
        final int end = Math.min(getChildrenCount(groupPosition), start + MAX_ITEMS_MEASURED);
        final int count = end - start;
        start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
        for (int i = start; i < end; i++) 
        {
            itemView = getChildView(groupPosition, i, (i+1 == end), itemView, viewGroup);
            if (itemView.getLayoutParams() == null)
            {
                itemView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            itemView.measure(widthMeasureSpec, heightMeasureSpec);
    
            if(i+1 != end)
            height += itemView.getMeasuredHeight();//Math.max(height, itemView.getMeasuredHeight());
        }
    
        return height;
    }
    

    Note: I had to adjust the code to exclude the last item's height because it was adding extra space below the last item. If you have children of different height, there might be trouble. I still get some empty space below the children/ group header, but it's excusable (unlike the previous amount, which was 3-4 children tall).

提交回复
热议问题