android - ListView inside ScrollView 滚动改进

标签 android listview scrollview

我可以看到一些关于“ListView inside ScrollView”的问题。而且我知道,我们不应该做这样的嵌套,只是因为两个组件都有自己的滚动并且谷歌这么说(我读过它是无用的东西)。但在我当前的项目中,我需要这样的行为:如果 ListView 可以滚动 - 它正在滚动,如果不能( ListView 的顶部或底部边框) - ScrollView 正在滚动。 所以,我写了这样的代码:

public static void smartScroll(final ScrollView scroll, final ListView list){
        scroll.requestDisallowInterceptTouchEvent(true);
        list.setOnTouchListener(new OnTouchListener() {
            private boolean isListTop = false, isListBottom = false;
            private float delta = 0, oldY = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {             
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    oldY = event.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    delta = 0;
                    break;
                case MotionEvent.ACTION_MOVE:
                    delta = event.getY() - oldY;
                    oldY = event.getY();

                    isListTop = false;
                    isListBottom = false;

                    View first = list.getChildAt(0);
                    View last = list.getChildAt(list.getChildCount()-1);                
                    if(first != null && list.getFirstVisiblePosition() == 0 && first.getTop() == 0 && delta > 0.0f){
                        isListTop = true;
                    }
                    if(last != null && list.getLastVisiblePosition() == list.getCount()-1 && last.getBottom() <= list.getHeight() && delta < 0.0f){
                        isListBottom = true;
                    }

                    if( (isListTop && delta > 0.0f) || (isListBottom && delta < 0.0f) ){
                        scroll.post(new Runnable() {
                            public void run() {
                                scroll.smoothScrollBy(0, -(int)delta);
                            }
                        });
                    }

                    break;
                default: break;
                }                   
                scroll.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    }

它可以工作,至少在目标 API 8 上是这样。但是有一些滚动故障(不自然的跳跃)。我认为,原因在 scroll.smoothScrollBy(0, -(int)delta);有没有人有想法,如何改进 scrollview 滚动 :)?它被称为经常(在移动中)和在岗位上,也许这就是原因?

最佳答案

我遇到了同样的问题。我改进了这段代码,现在我认为它可以正常工作了。

    public static void smartScroll(final ScrollView scroll, final ListView list){
    list.setOnTouchListener(new View.OnTouchListener() {
        private boolean isListTop = false, isListBottom = false;
        private float delta = 0, oldY = 0;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    scroll.requestDisallowInterceptTouchEvent(true);
                    list.requestDisallowInterceptTouchEvent(false);
                    oldY = event.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    delta = 0;
                    break;
                case MotionEvent.ACTION_MOVE:
                    delta = event.getY() - oldY;
                    oldY = event.getY();

                    isListTop = false;
                    isListBottom = false;

                    View first = list.getChildAt(0);
                    View last = list.getChildAt(list.getChildCount()-1);
                    if(first != null && list.getFirstVisiblePosition() == 0 && first.getTop() == 0 && delta > 0.0f){
                        isListTop = true;
                    }
                    if(last != null && list.getLastVisiblePosition() == list.getCount()-1 && last.getBottom() <= list.getHeight() && delta < 0.0f){
                        isListBottom = true;
                    }

                    if( (isListTop && delta > 0.0f) || (isListBottom && delta < 0.0f) ){
                        scroll.requestDisallowInterceptTouchEvent(false);
                        list.requestDisallowInterceptTouchEvent(true);
                    }
                    break;
                default: break;
            }
            return false;
        }
    });
}

当我们触摸 ListView 时 - 我们启用它的滚动并禁用父滚动:

                    scroll.requestDisallowInterceptTouchEvent(true);
                    list.requestDisallowInterceptTouchEvent(false);

如果我们到达列表的边界 - 我们禁用它的滚动并启用父滚动:

                    scroll.requestDisallowInterceptTouchEvent(false);
                    list.requestDisallowInterceptTouchEvent(true);

它对我来说滚动起来非常流畅。希望这会有所帮助。

关于android - ListView inside ScrollView 滚动改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905152/

相关文章:

android - 如何在 Android 中更新 SimpleAdapter

VB.Net ListView 如果宽度 = 0 则限制列调整大小

ios - 为 ScrollView 上的双指滑动分配独特的行为

android - 相对布局滚动问题

android - Gradle 属性的 "USER_HOME"文件夹在哪里?

android - 将图像保存到本地存储/从本地存储加载图像

java - 使用 StringBuilder 从提取的字符中构建字符串

android - 回到主要 Activity 的后退按钮引发 "Unfortunately Application has stopped"

c# - 在 Activity 之间共享数据(字符串),保存并将其添加到ListView C#Xamarin

快速捏缩放