在扩展列表视图的Android禁用自动滚屏(Android disable auto scroll

2019-08-17 04:41发布

我在扩张的ListView使用,当我在ListView打开的项目之一我的滚动自动聚焦打开的项目,我可以从列表中避免把重点放在新的项目,并留在同一个地方? 我已经尝试删除从打开的视图可对焦,但没有奏效。

Answer 1:

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

下面是一个实现的例子:

        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;
        }
    });

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

针对默认行为参考,我将发布它,从Android源代码采取(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;
}


Answer 2:

的哈克的方式的种类,但是可以延长ExpandableListView并覆盖其]平滑滚动的方法和不能做任何事情。 代码是C#,但你的基本思路:

{
    public class NonSmoothScrollExpandableListView: ExpandableListView
    {
        public NonSmoothScrollExpandableListView(Context context): base(context)
        {

        }

        public NonSmoothScrollExpandableListView(Context context, IAttributeSet attrs) :
            base(context, attrs)
            {
        }

        public NonSmoothScrollExpandableListView(Context context, IAttributeSet attrs, int defStyle) :
            base(context, attrs, defStyle)
            {
        }

        public override void SmoothScrollByOffset(int offset)
        {
        }

        public override void SmoothScrollBy(int distance, int duration)
        {
        }

        public override void SmoothScrollToPosition(int position)
        {
        }

        public override void SmoothScrollToPosition(int position, int boundPosition)
        {
        }

        public override void SmoothScrollToPositionFromTop(int position, int offset)
        {
        }

        public override void SmoothScrollToPositionFromTop(int position, int offset, int duration)
        {
        }
    }
}


文章来源: Android disable auto scroll in expandable list view