listview的item内部有Button问题

listview内部的item里如果有Button会导致listview的OnItemClickListener不触发,这个问题想必大家都知道,也知道怎么解决
在item跟布局里加一句android:descendantFocusability="blocksDescendants" 就搞定了。
stackover上也有另一种解决方法,在Button里加2行代码
android:focusable="false"

android:focusableInTouchMode="false"

其实还有一种解决方法,那就去去掉OnItemClickListener,在adapter内写view的onClickListenr,这个是不会被Button影响的。



三种方法都可以解决问题,但原因是什么,大部分文章都没说?今天简单看了下AbsListView里有这么一段代码。可以看出只有在child.hasFocusable()为false的情况下才会触发click事件,那么默认情况下Button是有focus的会导致他的parent也有focus,所以就无法触发click事件。

    private void onTouchUp(MotionEvent ev) {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);
            if (child != null) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                final float x = ev.getX();
                final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right;
                if (inList && !child.hasFocusable()) {
                    if (mPerformClick == null) {
                        mPerformClick = new PerformClick();
                    }
                    。。。
                    performClick.run();

再看看ViewGroup的hasFocusable代码就更明白了,如果不是FOCUS_BLOCK_DESCENDANTS,那只要有一个child hasFocusable,那直接返回true,而Button就是focusable的。而设置了FOCUS_BLOCK_DESCENDANTS之后,那么就返回false了

    public boolean hasFocusable() {
        if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
            return false;
        }

        if (isFocusable()) {
            return true;
        }

        final int descendantFocusability = getDescendantFocusability();
        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
            final int count = mChildrenCount;
            final View[] children = mChildren;

            for (int i = 0; i < count; i++) {
                final View child = children[i];
                if (child.hasFocusable()) {
                    return true;
                }
            }
        }

        return false;
    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值