Question Detail

How to Scroll ListView inside a ScrollView in android?

6 years ago Views 3248 Visit Post Reply

I have a Linearlayout which has a scrollView with a number of elements within it. At the bottom of the scrollView I have a listView which is then populated by an adapter.

It is showing me only first element of ListView.

The problem that I am experiencing, is that Android is excluding the listView from the scrollView as the scrollView already has a scrollable function. I want the listView to be as long as the content is and for the master scroll view to be scrollable.

How can I achieve this behavior?


Thread Reply

Anonymous

- 6 years ago

To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true" to its XML declaration or by explicitly calling setNestedScrollingEnabled(true).


For Pre-Lolipop Devices
First you have to replace you ScrollView with NestedScrollView. The latter implements both NestedScrollingParent and NestedScrollingChild so it can be used as a parent or a child scroll container.

 

 

But ListView doesn't support nested scrolling, therefore you need to subclass it and implement NestedScrollingChild. Fortunately, the Support library provides NestedScrollingChildHelper class, so you just have to create an instance of this class and call its methods from the corresponding methods of your view class.

Anonymous

- 6 years ago

you can try this :
What this does is disable the TouchEvents on the ScrollView and make the ListView intercept them. It is simple and works all the time.

ListView listview = (ListView)findViewById(R.id.ListView_ID);  // listview inside scrollview
listview.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });