天天看點

Scroll嵌套ListView的小問題ListView在ScrollView嵌套問題

ListView在ScrollView嵌套問題

Dont put ListView inside of a ScrollView

如果一定要這麼做的話,可能會涉及到如下兩點問題

ListView隻顯示第一個item,沒有展開

這裡涉及到ListView的繪制過程,産生問題的原因在ListView#onMeasure方法中。

現在我們有一個ListView,配置如下

<ListView
    android:id="@+id/scroll_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
           

一般來說,wrap_content對應的specMode是AT_MOST,但是在ScrollView中則是UNSPECIFIED,是以導緻了ListView顯示不正常

D/Measure Model: onMeasure widthMode: EXACTLY
D/Measure Model: onMeasure heightMode: UNSPECIFIED
D/Measure Model: onMeasure widthMode: EXACTLY
D/Measure Model: onMeasure heightMode: UNSPECIFIED
           

是以有第一種解決方法,指定ListView的layout_height,這時specMode是EXACTLY,ListView會按指定高度繪制

<ListView
	android:layout_width="match_parent"
	android:layout_height="xxxdp"/>
           

然後在去找ListView#onMeasure方法,看一下在specMode為UNSPECIFIED的情況下,ListView如何測算高度

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	...
    if (mItemCount > 0 && (widthMode == MeasureSpec.UNSPECIFIED
    || heightMode == MeasureSpec.UNSPECIFIED)) {
        final View child = obtainView(0, mIsScrap);

        // Lay out child directly against the parent measure spec so that
        // we can obtain exected minimum width and height.
        measureScrapChild(child, 0, widthMeasureSpec, heightSize);

        childWidth = child.getMeasuredWidth();
        childHeight = child.getMeasuredHeight();
        ...
    }
    ...
    if (heightMode == MeasureSpec.UNSPECIFIED) {
        heightSize = mListPadding.top + mListPadding.bottom + childHeight +
        getVerticalFadingEdgeLength() * 2;
    }
}
           

根據onMeasure源碼不難看出,specMode指定為UNSPECIFIED時,隻使用了一個childHeight,并不是所有childHeight。

據此我們可以動态計算ListView高度,利用LayoutParams重新設定layout_height,貼一個解決方案

好像隻需要把specMode換成AT_MOST就可以了

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
           

ListView不能滑動

這個涉及到Android的事件分發,本來以為是由于parent layout攔截touch event,導緻事件在到達ListView前就被消耗。但是實際上測試一下發現并不是這樣

I/EVENT: onDispatchEvent ACTION_DOWN: deal with ScrollView
I/EVENT: onInterceptTouchEvent ACTION_DOWN: judge by ScrollView
I/EVENT: onDispatchEvent ACTION_DOWN: deal with ListView
I/EVENT: onInterceptTouchEvent ACTION_DOWN: judge by ListView
I/EVENT: onTouchEvent ACTION_DOWN: deal with ListView
I/EVENT: onDispatchEvent ACTION_MOVE: deal with ScrollView
I/EVENT: onInterceptTouchEvent ACTION_MOVE: judge by ScrollView
I/EVENT: onDispatchEvent ACTION_MOVE: deal with ListView
I/EVENT: onTouchEvent ACTION_MOVE: deal with ListView
I/EVENT: onDispatchEvent ACTION_MOVE: deal with ScrollView
I/EVENT: onInterceptTouchEvent ACTION_MOVE: judge by ScrollView
I/EVENT: onDispatchEvent ACTION_CANCEL: deal with ListView
I/EVENT: onTouchEvent ACTION_CANCEL: deal with ListView
I/EVENT: onDispatchEvent ACTION_MOVE: deal with ScrollView
I/EVENT: onTouchEvent ACTION_MOVE: deal with ScrollView
I/EVENT: onDispatchEvent ACTION_MOVE: deal with ScrollView
I/EVENT: onTouchEvent ACTION_MOVE: deal with ScrollView
           

可以看到一開始

ACTION_DOWN

确實發送到了ListView中,但是ListView并沒有接收完整的touch event,随後的

ACTION_MOVE

被ScrollView改寫為

ACTION_CANCEL

發送到了ListView,導緻ListView touch event的異常結束,該touch event被ScrollView接管。這一過程在使用者看來,表現為ListView沒有滑動。

貼一個requestDisallow解決方案。

利用

getParent().requestDisallowInterceptTouchEvent(boolean)

控制父ViewGroup的事件分發。根據Android事件分發的過程,這段代碼寫在dispatchTouchEvent裡確定執行,由于ACTION_DOWN一定會到ListView的onTouchEvent中執行,是以我把代碼放在了onTouchEvent中。

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();

    switch (action){
    case MotionEvent.ACTION_DOWN:{
        getParent().requestDisallowInterceptTouchEvent(true);
        ...
        break;
    }
    case MotionEvent.ACTION_MOVE:{
    	...
        break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        getParent().requestDisallowInterceptTouchEvent(false);
        break;
    }

    return super.onTouchEvent(ev);
}
           

然後繼續分析輸出日志,對于

ACTION_DOWN

來說,傳遞過程就是标準的事件分發過程,但是對于

ACTION_MOVE

而言,ListView并沒有對其攔截判斷,而是直接調用onTouchEvent進行處理。

在ViewGroup#dispatchTouchEvent源碼中可知,onInterceptTouchEvent并不是一定被調用的。隻有滿足

actionMasked == MotionEvent.ACTION_DOWN

mFirstTouchTarget != null

存在子view可以接收touch event時,才會判斷是否攔截;否則該ViewGroup會持續攔截接受到的touch event

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
	...
    // Check for interception.
    final boolean intercepted;
    if (actionMasked == MotionEvent.ACTION_DOWN
    || mFirstTouchTarget != null) {
        final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
        if (!disallowIntercept) {
            intercepted = onInterceptTouchEvent(ev);
            ev.setAction(action); // restore action in case it was changed
        } else {
            intercepted = false;
        }
    } else {
        // There are no touch targets and this action is not an initial down
        // so this view group continues to intercept touches.
        intercepted = true;
    }
}
           

具體到ListView來說,當

ACTION_MOVE

傳遞到ListView時,已經是傳遞鍊末端,故

mFirstTouchTarget

為null,是以ListView并沒有調用onInterceptionTouchEvent,而是直接處理。