1. ListView 數(shù)據(jù)只顯示一條
ScrollView 里直接嵌套 ListView 時(shí),數(shù)據(jù)只能顯示一條,通常會(huì)重寫 ListView 的 onMeasure() 方法,也可以重新計(jì)算高度。
1.1 ListView 中的 item 高度固定
1.1.1 繼承 ListView 重寫 onMeasure() 方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
1.1.2 手動(dòng)計(jì)算 ListView 的高度
public static void setListViewHeightBasedOnChildren(ListView listView) {
//獲取ListView對(duì)應(yīng)的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回?cái)?shù)據(jù)項(xiàng)的數(shù)目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); //計(jì)算子項(xiàng)View 的寬高
totalHeight += listItem.getMeasuredHeight(); //統(tǒng)計(jì)所有子項(xiàng)的總高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//listView.getDividerHeight()獲取子項(xiàng)間分隔符占用的高度
//params.height最后得到整個(gè)ListView完整顯示需要的高度
listView.setLayoutParams(params);
}
1.2 最后一個(gè) item 顯示不全的問(wèn)題
1.2.1 取消設(shè)置 ListView 的 divider 高度
這里我沒(méi)有試過(guò)方法 2,但是在使用方法 1 重寫 onMeasure() 時(shí),在 小米 5 手機(jī)上遇到了最后一個(gè) item 始終顯示不全的問(wèn)題,但在另外幾個(gè)測(cè)試機(jī)上是正常顯示的。
后來(lái)在谷歌搜索到,原來(lái)是給 ListView 設(shè)置了 divider 高度導(dǎo)致的,因?yàn)橹貙?onMeasure() 時(shí)沒(méi)有計(jì)算 divider 的高度,去掉這個(gè)設(shè)置就行了。
如果 item 高度不固定,也可以使用下述方法 1.2.2 。
1.2.2 ListView 中的 item 高度不固定,自定義 ListView
public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
注:1.1.1 和 1.2.1 是實(shí)際碰到的問(wèn)題和使用的方法。1.1.2 和 1.2.2 待驗(yàn)證,記錄一下筆記。
參考: