1

I have the following layout (for brevity sake, I removed the different attributes):

<RelativeLayout>
    <LinearLayout>
        <android.support.v7.widget.Toolbar/>
        <LinearLayout android:id="@+id/item1"/>
        <LinearLayout android:id="@+id/item2"/>
        <LinearLayout android:id="@+id/item3"/>
        <ListView/>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton/>
</RelativeLayout>

I am trying to get this whole page to be scrollable. At the moment, only the ListView scrolls. Any idea how to do? Note that I cannot use ScrollView due to the fact that I embed a ListView. Also, the outer element must be RelativeLayout as I need the FloatingActionButton to be fixed regardless of scrolling.

checklist
  • 12,340
  • 15
  • 58
  • 102
  • 1
    One direction I am considering: addHeaderView(View) in which I can add items 1,2.3. The toolbar can be fixed. – checklist Jun 24 '15 at 10:16

3 Answers3

3

Having a ListView inside a ScrollView would cause you a lots of trouble. Instead leave only the ListView in your layout

<RelativeLayout>
    <LinearLayout>
        <android.support.v7.widget.Toolbar/>
        <ListView/>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton/>
</RelativeLayout>

and move your LinearLayouts into separate file (for example header.xml)

<LinearLayout>
    <LinearLayout android:id="@+id/item1"/>
    <LinearLayout android:id="@+id/item2"/>
    <LinearLayout android:id="@+id/item3"/>
</LinearLayout>

You can then add the layout as header to your ListView with

ListView listView = (ListView) findViewById(R.id.list);
View header = LayoutInflater.from(context).inflate(R.layout.header, listView, false);
listView.addHeaderView(header); // call before you set adapter!

Now the whole view will be scrollable. If you use onItemClickListener don't forget that the index is shifted by listView.getHeaderViewsCount().

Lamorak
  • 10,957
  • 9
  • 43
  • 57
0

Using a vertical scrollable inside vertical scrollable is always messy and might just not work, but you can chceck LINK

also you can use listView.requestDisallowInterceptTouchEvent(false).

Community
  • 1
  • 1
M.Baraka
  • 725
  • 1
  • 10
  • 24
-1

I think that the only way to make a layout scrollable is to put all of the content into a ScrollView for example like this:

<RelativeLayout>
    <ScrollView android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout>
            <android.support.v7.widget.Toolbar/>
            <LinearLayout android:id="@+id/item1"/>
            <LinearLayout android:id="@+id/item2"/>
            <LinearLayout android:id="@+id/item3"/>
            <ListView/>
        </LinearLayout>
        <android.support.design.widget.FloatingActionButton/>
    </ScrollView>
</RelativeLayout>
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • a ListView cannot be inside a ScrollView. – checklist Jun 24 '15 at 10:07
  • you can use listView inside of a scrollView if you copy the setListViewHeightBasedOnChildren(lv) method in your code and with this you could expand the listView. Another possible solution is to simplify your layout and to put the layouts you want to be scrolled too in the listVeiw as headers or footers – Gabriella Angelova Jun 24 '15 at 10:11