ListView inside ScrollView scroll improvement

2019-08-12 18:10发布

问题:

I can see some questions about "ListView inside ScrollView" issue. And I know, that we shouldn't do such nesting, just because both components have their own scrolling and Google said so(I've read that it is useless thing). But in my current project I need such behavior: if listview can scroll - it is scrolling, if not(top or bottom border of listview) - scrollview is scrolling. So, I've wrote such code:

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

And it works, at least on target API 8. But there are some scroll glitches(unnatural jumps). I think, the cause in scroll.smoothScrollBy(0, -(int)delta); Have anybody thoughts, how to improve scrollview scrolling :)? It is called often(on move) and on post, maybe that is the cause?

回答1:

I have faced same problem. I've improved this code and now it works correct i think.

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

When we touch listview - we enable its scroll and disable parent scroll:

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

And if we reaching the border of the list - we disable its scroll and enable parent scroll :

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

It scrolls pretty smoothy for me. Hope this helps.



回答2:

If the parent is ScrollView , change it as LinearLayout. It will work. For example , in here I wrote LinearLayout instead of ScrollView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll" 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:fillViewport="true">

        <ListView android:id="@+id/fontlistView" 
            android:scrollbars="vertical"
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:layout_gravity="center_horizontal" 
            android:scrollbarAlwaysDrawVerticalTrack="true">
        </ListView>
</LinearLayout>


回答3:

My sugesstion would put the listview tag out side the scrollview. And the add the area outside the listview inside a scrollview. Then put everything inside a linear layout. So the scollview and listview will be seperate.



回答4:

i think u need this

listview = (ListView) findViewById(R.id.search_list);
listview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);

                }
                return false;
            }
        });