Skip to content Skip to sidebar Skip to footer

Listview Inside Scrollview Scroll Improvement

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 s

Solution 1:

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

publicstaticvoidsmartScroll(final ScrollView scroll, final ListView list){
    list.setOnTouchListener(newView.OnTouchListener() {
        privatebooleanisListTop=false, isListBottom = false;
        privatefloatdelta=0, oldY = 0;
        @OverridepublicbooleanonTouch(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;

                    Viewfirst= list.getChildAt(0);
                    Viewlast= 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;
            }
            returnfalse;
        }
    });
}

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.

Solution 2:

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

<LinearLayoutxmlns: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"><ListViewandroid: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>

Solution 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.

Solution 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);
                } elseif (event.getAction() == MotionEvent.ACTION_UP) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);

                }
                returnfalse;
            }
        });

Post a Comment for "Listview Inside Scrollview Scroll Improvement"