3
  • I'm having issue with rendering a scroll view, below which I want a list view.
  • I want a scroll view, inside which I will add image views and text views or may be other UI elements.
  • Below this scroll view, I want a list view.

But when I do this, either the list view and scroll view overlap, or only the scroll view is rendered.

XML Code: Gist

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.dell.finalstartup.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <!-- For pic of the day -->
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/layout_PicOfTheDay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pic of the day example layout"
                    android:textSize="22sp" />

                <ImageView
                    android:id="@+id/picOfTheDay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <!-- Grey Line -->
            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/gray_line_width"
                android:background="#c0c0c0" />

        </LinearLayout>
    </ScrollView>

    <!-- For products -->
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/scrollView" />

</RelativeLayout>
Bugs Buggy
  • 1,514
  • 19
  • 39
  • add both inside NestedScrollview – Divyesh Patel Jan 09 '17 at 08:00
  • @Divyesh did you mean like this? https://gist.github.com/AseedUsmani/190df5ff885109c34dd8b2cc7520bc2e I then get this error - `ScrollView can host only one direct child` Although, `ScrollView` that I used has only one direct child, a linear layout. – Bugs Buggy Jan 09 '17 at 08:12
  • first create nestedScrollview, inside it create Verticle Linear layout. now add both scrollview and listview inside that linear layout – Divyesh Patel Jan 09 '17 at 08:13
  • @Divyesh when I do that, I'm getting only 1 item inside the list view, even though I am fetching 2 items in JSON Array, even logs show two values, and both of them should be added in the adapter in the `onPostExecute()` method – Bugs Buggy Jan 09 '17 at 08:22

2 Answers2

1

Its always an issue to have multiple scroll based views in the same layout. You can use a nested scrollview which is part of the V4 library.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>

For more on layout_behaviour and nested scroll.

amalBit
  • 12,041
  • 6
  • 77
  • 94
  • https://gist.github.com/AseedUsmani/4e8b9faf0ddde73f16724679d72ec3f1 When I use this XML, even though I have 2 objects in the JSON that I fetch, only 1 is added in the list View even though in the logs I get two objects. – Bugs Buggy Jan 09 '17 at 08:30
  • @AseedUsmani Nested scrollview accepts multiple children.. something like this https://gist.github.com/amalChandran/3d65e914ddc675b841cdedc86163daa8 – amalBit Jan 09 '17 at 08:36
  • When I used your gist, I got the same error, ScrollView can have only one direct child, so I added a LinearLayout inside that and this again gives the same problem, only 1 object is being added to list view; to cross check, I started a new activity using the same code with just the list view, there I get both the objects. – Bugs Buggy Jan 09 '17 at 08:59
  • Did you try scrolling that one view? Ill check in detail and get back. – amalBit Jan 09 '17 at 09:00
  • Yes, I did. It wouldn't scroll after the first item in listView – Bugs Buggy Jan 09 '17 at 09:01
  • Nested scrollview works best with recyclerview. For the one row issue with listview, check the answer on this SO post. http://stackoverflow.com/q/34146041/2219600 – amalBit Jan 09 '17 at 09:28
  • Thanks, this is what I got now...progress! http://imgur.com/a/yfYfk The screenshot without image is what the data looks like, the one with image is what I get. I tried removing all paddings, adding padding in list view, scroll view etc – Bugs Buggy Jan 09 '17 at 11:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132752/discussion-between-amalbit-and-aseedusmani). – amalBit Jan 10 '17 at 06:03
0

Change the Relative layout to Linear layout will solve this issue..!!

lakshmi
  • 9
  • 4