Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在可扩展列表视图中禁用自动滚动_Android_Scrollview_Expandablelistview - Fatal编程技术网

Android在可扩展列表视图中禁用自动滚动

Android在可扩展列表视图中禁用自动滚动,android,scrollview,expandablelistview,Android,Scrollview,Expandablelistview,我在可展开的listview中使用,当我在listview中打开其中一个项目时,我的滚动会自动聚焦于打开的项目,我是否可以防止列表聚焦于新项目并停留在同一位置?我试图从open的视图中删除可聚焦,但没有成功。您需要覆盖OnGroupClickListener并使用事件。这将避免ExpandableListView执行默认操作,至少在针对4.3进行编译时是平滑滚动到该位置 下面是一个实现示例: expandableListView.setOnGroupClickListener(n

我在可展开的listview中使用,当我在listview中打开其中一个项目时,我的滚动会自动聚焦于打开的项目,我是否可以防止列表聚焦于新项目并停留在同一位置?我试图从open的视图中删除可聚焦,但没有成功。

您需要覆盖OnGroupClickListener并使用事件。这将避免ExpandableListView执行默认操作,至少在针对4.3进行编译时是平滑滚动到该位置

下面是一个实现示例:

        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)


有点老套,但您可以扩展
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)
        {
        }
    }
}
{
    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)
        {
        }
    }
}