ScrollView包裹ExpanableListView显示不全问题,简单有效的解决方法

       最近需要搞一个三级菜单,最开始是打算用ExpandableListView包裹ExpandableListView实现的,粗略写完后发现直接崩溃了,方法应该是可以的,但还需要时间去调试,因为时间有点赶,我就直接用了ScrollView和ExpandableListView搭配,通过addView的动态方式,算是实现了这个三级菜单,因为这个方式View的可复用性实在太差,我做完也没有花时间模仿重用View进行优化,所以这个就分享出来了。
        主要是说一下ScrollView包裹ExpanableListView显示不全问题的解决方法。
        正常直接将ExpandableListView放进ScrollView里面,一般只有第一个groupItem可以看到
 

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <ExpandableListView
            android:id="@+id/lvTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ExpandableListView>
        
</ScrollView>

出来的效果就是这样
 

最简单的解决方法就是重写ExpandableListView的onMeasure方法
 

package cn.ALeeCJ.learningproject.scrollview_expandablelistview_test;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

public class ResolveDisplayProblemExpandableListView extends ExpandableListView {
    public ResolveDisplayProblemExpandableListView(Context context) {
        super(context);
    }

    public ResolveDisplayProblemExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ResolveDisplayProblemExpandableListView(Context context, AttributeSet attrs,
                                                   int defStyle) {
        super(context, attrs, defStyle);
    }

    //重写这个方法,即可解决ScrollView包裹ExpandableListView显示不全问题
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

之后布局文件换成重写方法后的ExpandableListView(我这就是ResolveDisplayProblemExpandableListView)

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <cn.ALeeCJ.learningproject.scrollview_expandablelistview_test.ResolveDisplayProblemExpandableListView
                android:id="@+id/lvTest"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        </cn.ALeeCJ.learningproject.scrollview_expandablelistview_test.ResolveDisplayProblemExpandableListView>

</ScrollView>


十分的简单,在这就不多哔哔了,直接看效果图
 

到这里就结束了。

发布了24 篇原创文章 · 获赞 2 · 访问量 3675

猜你喜欢

转载自blog.csdn.net/ALee_130158/article/details/91039994
今日推荐