ExpandableListView使用(三)-ScrollView嵌套ExpandableListView,列表显示不全

本文介绍了如何处理ScrollView内嵌ExpandableListView导致列表显示不全的常见问题,提出通过复写ExpandableListView的onMeasure()方法来实现理想的显示效果,同时讨论了设置android:fillViewport="true"的不足,并预告了计算列表项高度的复杂方法作为备选方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

ScrollView嵌套ExpandableListView会出现ExpandableListView列表显示不全,目前比较好的方法是复写ExpandableListView,重写里面的onMeasure()方法,由于复写的东西并也不多,所以这种方式比较理想

示例

SuperExpandableListView.java

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

/**
 * 解决ScrollView嵌套ExpandableListView时,ExpandableListView显示不全的问题
 * Created by 万紫辉 on 2017/6/13
 * Email:wzhghgg@gmail.com
 */

public class SuperExpandableListView extends ExpandableListView {

    public SuperExpandableListView(Context context) {
        super(context);
    }

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

    public SuperExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

使用还是跟原来的ExpandableListView的使用方式是一样的

activity_group_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/gray_light"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <SuperExpandableListView
                android:id="@+id/elv_teacher_grade"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

注意

有种方式是设置SrcollView的android:fillViewport=”true” ,这样虽然ExpandableListView可以显示完全,但是ExpandableListView不能滑动,所以这种不能完全解决问题;所以最好的方式是复写ExpandableListView。

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/gray_light"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

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

后续

也有另外一直方法,就是计算ExpandableListView每个列表项的高度,从而确定列表的高度;但是这种方式比较复杂,可以参考,后面会贴出改方法

有效参考

ScrollView嵌套ExpandableListView显示不正常的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值