getChildView not being called

前端 未结 3 767
余生分开走
余生分开走 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:25

    The Problem is, your ExpandableListView is in a ScrollView. This is most of the times the result of bad layouts, and you should try to avoid it.

    However sometimes it is the only solution (that's actually not true but other solutions might be out of ones scope). Your solution also works but is a bit complex. Instead write a custom ExpandableListView like this:

    public class ExpandExpandableListView extends ExpandableListView{
        boolean expanded = true;
    
        public ExpandExpandableListView(Context context) {
            super(context);
        }
    
        public ExpandExpandableListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ExpandExpandableListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public boolean isExpanded()
        {
            return expanded;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (isExpanded())
            {
                // Calculate entire height by providing a very large height hint.
                // View.MEASURED_SIZE_MASK represents the largest height possible.
                int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, expandSpec);
    
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            }else{
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    
        public void setExpanded(boolean expanded)
        {
            this.expanded = expanded;
        }
    }
    

    and in your m_backdrop.xml:

    
    
            
    
            
    
    
    

    That's already it. It should work as expected.

提交回复
热议问题