Scroll嵌套ListView的小问题

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_DOWNmFirstTouchTarget != 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,而是直接处理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值