安卓禁用自动滚动在扩展列表视图 [英] Android disable auto scroll in expandable list view

查看:177
本文介绍了安卓禁用自动滚动在扩展列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在扩展列表视图使用的,当我在列表视图中打开的项目我的一个滚动的自动对焦打开的项目,我可以prevent从列表中把重点放在新的项目,并保持在同一地点?我已经尝试从打开的视图中删除可获得焦点,但没有奏效。

i use in expandable listview and when i open one of the items in the listview my scroll is automatically focus on the item that open, can i prevent from the list to focus on the new item and stay in the same place? i've try to remove the focusable from the open's view but it didn't work.

推荐答案

您需要重写OnGroupClickListener和消费活动。这将避免ExpandableListView做的默认操作,其中至少对4.3编译时是smoothScroll的位置。

You need to override the OnGroupClickListener and consume the event. This will avoid the ExpandableListView doing the default action, which at least when compiling against 4.3 is to smoothScroll to the position.

下面是一个实现的例子:

Here is an implementation example:

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            if(parent.isGroupExpanded(groupPosition)){
                parent.collapseGroup(groupPosition);
            }else{
                boolean animateExpansion = false;
                parent.expandGroup(groupPosition,animateExpansion);
            }
            //telling the listView we have handled the group click, and don't want the default actions.
            return true;
        }
    });

如果你消耗的情况下,有你需要的,如果你需要它来复制一些默认的行为。这包括播放声音效果,并呼吁展开/折叠-听众。

If you consume the event, there is some default behavior you need to replicate if you need it. This includes playing sound effects and calling expand/collapse-listeners.

有关对默认行为的参考,我将它张贴,从Android源$ C ​​$ C取(4.3)

For reference against the default behavior, I will post it, taken from the Android source code (4.3)

    //in ExpandableListView.java 
    boolean handleItemClick(View v, int position, long id) {
    final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position);

    id = getChildOrGroupId(posMetadata.position);

    boolean returnValue;
    if (posMetadata.position.type == ExpandableListPosition.GROUP) {
        /* It's a group, so handle collapsing/expanding */

        /* It's a group click, so pass on event */
        if (mOnGroupClickListener != null) {
            if (mOnGroupClickListener.onGroupClick(this, v,
                    posMetadata.position.groupPos, id)) {
                posMetadata.recycle();
                return true;
            }
        }

        if (posMetadata.isExpanded()) {
            /* Collapse it */
            mConnector.collapseGroup(posMetadata);

            playSoundEffect(SoundEffectConstants.CLICK);

            if (mOnGroupCollapseListener != null) {
                mOnGroupCollapseListener.onGroupCollapse(posMetadata.position.groupPos);
            }
        } else {
            /* Expand it */
            mConnector.expandGroup(posMetadata);

            playSoundEffect(SoundEffectConstants.CLICK);

            if (mOnGroupExpandListener != null) {
                mOnGroupExpandListener.onGroupExpand(posMetadata.position.groupPos);
            }

            final int groupPos = posMetadata.position.groupPos;
            final int groupFlatPos = posMetadata.position.flatListPos;

            final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); 
            smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos),
                    shiftedGroupPosition);
        }

        returnValue = true;
    } else {
        /* It's a child, so pass on event */
        if (mOnChildClickListener != null) {
            playSoundEffect(SoundEffectConstants.CLICK);
            return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos,
                    posMetadata.position.childPos, id);
        }

        returnValue = false;
    }

    posMetadata.recycle();

    return returnValue;
}

这篇关于安卓禁用自动滚动在扩展列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆