1. 程式人生 > >ExpandableListView使用(三)-ScrollView嵌套ExpandableListView,列表顯示不全

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

ren 不能 viewport make ext line ray esp vertica

前言

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:[email protected]
 */

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每個列表項的高度,從而確定列表的高度;但是這種方式比較復雜,可以參考,後面會貼出改方法

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