Search code examples
androidxmlandroid-layoutscrollview

How can i add double ScrollView in XML?


I am currently trying to add a double ScrollView inside my XML. I am using two different views in my layout - a list and categories.

When a user clicks on a category - the list of videos will become visible and the categories will become invisible. Currently, the category buttons are scroll-able and when I click a category button, the list of videos does become visible. However, the issue is that the list of videos do not scroll.

Here is my layout code:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/tools"
        android:orientation="vertical"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--this is parent layout where I call ListView-->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/parent01"
            android:descendantFocusability="beforeDescendants"
            android:fitsSystemWindows="true"
            android:focusableInTouchMode="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical"
            android:weightSum="1">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ListView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="5dp"
                    android:divider="@android:color/transparent"
                    android:dividerHeight="1dp"></ListView>

            <ProgressBar
                    android:id="@+id/nextProgress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_gravity="center"
                    android:visibility="gone"
                    style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Small"/>
        </RelativeLayout>
    </LinearLayout>

    <!--This is the categories layout-->
    <ScrollView
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ll_buttons"
            android:layout_marginTop="60dp"
            android:layout_height="wrap_content">

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

            <Button
                    android:layout_weight="0.5"
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"/>

            <Button
                    android:layout_weight="0.5"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"
                    android:id="@+id/button2"/>
        </LinearLayout>

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

            <Button
                    android:layout_weight="0.5"
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"/>

            <Button
                    android:layout_weight="0.5"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"
                    android:id="@+id/button4"/>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Solution

  • for nested scrolling better to use NestedScrollView

    NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view.

    ScrollView vs NestedScrollView

    another thing use RecyclerView insted of Listview

    RecyclerView vs. ListView

    sample layout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
     <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
    
            // add here all your controlls
    
    
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    and dont forgot to set setNestedScrollingEnabled() property in RecyclerView like below code

    If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll.

    mRecyclerView.setNestedScrollingEnabled(false);