0

I have a RelativeLayout and below that i have a ListView. I have to place these inside a ScrollView. Any help will be deeply appreciated.

simonc
  • 39,997
  • 11
  • 78
  • 100
  • Place the `RelativeLayout` view as the header view of the list. – user Nov 23 '12 at 09:35
  • [Duplicated](http://stackoverflow.com/q/5415011/1050058) , [Duplicated](http://stackoverflow.com/q/4651793/1050058) – Trung Nguyen Nov 23 '12 at 09:39
  • you should not put a listView inside a scrollView. the listView implement its own scrollListener , and it will not responds to events on the scrollView, – Houcine Nov 23 '12 at 09:39

4 Answers4

2

You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView

Ram kiran
  • 20,129
  • 14
  • 55
  • 74
0

ScrollView can be set only for a single Layout. So place the widgets which u want to scroll in a single layout as below:

<ScrollView
    android:id="@+id/scrView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >

    <RelativeLayout
        android:id="@+id/rltvLyt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/lstView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp" />
    </RelativeLayout>
</ScrollView>
Avadhani Y
  • 7,276
  • 18
  • 55
  • 90
0

Don't do it. What exactly are you trying to achieve? It had been mentioned here (and everywhere else) this is not a good idea because the scrolling mechanisms get confused as they both want to scroll. Ignoring you however have a listview that is massive then you have missed the major performance gain of using a listview and might swell be using a table layout and add rows.

Paul Harris
  • 5,583
  • 1
  • 22
  • 41
  • Your answer is same as Ram kiran. And if you see this is duplicated, flag question. – Trung Nguyen Nov 23 '12 at 09:34
  • We posted at the same time. Also it might be that what he wants is to use a table layout so I am trying to figure it out because it's possible he is just asking the wrong question in which case we can edit it. I am all for closing questions but sometimes this community is far to happy to do it when all that was wrong is they asked the wrong question and it looks like a duplicate. – Paul Harris Nov 23 '12 at 09:39
0

You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView. I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers.

private void setListViewScrollable(final ListView list) {
        list.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                listViewTouchAction = event.getAction();
                if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                {
                    list.scrollBy(0, 1);
                }
                return false;
            }
        });
        list.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view,
                    int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                {
                    list.scrollBy(0, -1);
                }
            }
        });
    }

listViewTouchAction is a global integer value. If you can replace the line

 list.scrollBy(0, 1);
Jatin Patel
  • 1,603
  • 2
  • 15
  • 34