代碼如下所示
package com.lanbang.material.views; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /** * 重寫ListView,避免在SrollView裏面的ListView無法全部展示數據,(避免只能展示一行,必須靠滾動條來滾動查看所有數據) * * */ public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context) { super(context); } public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override /** * 重寫該方法,達到使ListView適應ScrollView的效果 * */ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }如果是ExpandableListView嵌套在ScrollView裏面,代碼一樣,只是將ListView修改爲ExpandableListView,因爲ExpandableListView是ListView的子類。
親測可以實現。