How to disable scroll view for list view and increase its height of list view as cells increase?

Viewed 292

I have an activity containing some textviews and buttons in scroll view . I have another list view in same layout which has default scroll view. Now scroll view for that list view should be disabled and height go scroll view should increase dynamically as the rows or cells increase and parent scroll view should be applied for textviews, buttons, list views.

Here is my xml code:

<?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:background="#efe9ef"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    tools:context="com.bodaty.samyata.samyata1.com.bodaty.samyata.samyata2.deyaPay.SuccessPage">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
       >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_marginTop="20dp"
            android:fontFamily="@font/poppinsmedium"
            android:text="ORDER DETAILS:"
            android:textColor="#000000"
            android:textSize="14sp" />
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="10dp"
                android:orientation="vertical"
                android:padding="@dimen/activity_horizontal_margin"
                app:cardCornerRadius="4dp"
                app:cardElevation="2dp"
                tools:targetApi="n">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#ffffff"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_marginTop="8dp"

                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="170dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:fontFamily="@font/poppinsmedium"
                            android:gravity="start"
                            android:text="@string/sub_total"
                            android:textAlignment="textStart"
                            android:textColor="#58595d"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:layout_width="55dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:gravity="end"
                            android:text="@string/dolor"
                            android:fontFamily="@font/poppinsmedium"
                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:id="@+id/pq"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="20dp"
                            android:layout_marginStart="10dp"
                            android:fontFamily="@font/poppinsmedium"
                            android:gravity="end"
                            android:lines="1"
                            android:text="@string/price"
                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            tools:targetApi="n" />
                    </LinearLayout>


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_marginTop="0dp"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="170dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:fontFamily="@font/poppinsmedium"
                            android:gravity="start"
                            android:text="@string/tax"
                            android:textAlignment="textStart"
                            android:textColor="#58595d"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:layout_width="53dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:fontFamily="@font/poppinsmedium"
                            android:gravity="end"
                            android:text="@string/dolor"

                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:id="@+id/taxtopay"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="20dp"
                            android:layout_marginStart="10dp"
                            android:gravity="end"
                            android:lines="1"
                            android:text="@string/price"
                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            android:fontFamily="@font/poppinsmedium"

                            tools:targetApi="n" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="0dp"
                        android:orientation="horizontal"
                        android:visibility="gone"
                        android:id="@+id/dc">

                        <TextView
                            android:layout_width="170dp"
                            android:layout_height="match_parent"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:fontFamily="@font/poppinsmedium"
                            android:gravity="start"
                            android:text="delivery charges :"
                            android:textAlignment="textStart"
                            android:textColor="#58595d"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:gravity="end"
                            android:text="@string/dolor"
                            android:textAlignment="textEnd"
                            android:textColor="#000000"
                            android:textSize="14sp"
                            android:fontFamily="@font/poppinsmedium"

                            tools:targetApi="n" />

                        <TextView
                            android:id="@+id/cost1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="20dp"
                            android:layout_marginStart="10dp"
                            android:gravity="end"
                            android:lines="1"
                            android:fontFamily="@font/poppinsmedium"
                            android:text="@string/price"
                            android:textAlignment="textEnd"
                            android:textColor="#000000"
                            android:textSize="14sp"
                            tools:targetApi="n" />
                    </LinearLayout>


                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0.5dp"
                        android:layout_marginEnd="10dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginStart="10dp"
                        android:background="#808080">

                    </View>


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="8dp"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="170dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:gravity="start"
                            android:fontFamily="@font/poppinsmedium"
                            android:text="@string/total_price"
                            android:textAlignment="textStart"
                            android:textColor="#000"
                            android:textSize="14sp"
                            tools:targetApi="n" />


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_weight="1"
                            android:gravity="end"
                            android:text="@string/dolor"
                            android:fontFamily="@font/poppinsmedium"

                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            tools:targetApi="n" />

                        <TextView
                            android:id="@+id/amountpaid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="20dp"
                            android:layout_marginStart="10dp"
                            android:gravity="end"
                            android:lines="1"
                            android:text="@string/price"
                            android:textAlignment="textEnd"
                            android:textColor="#000"
                            android:textSize="14sp"
                            android:fontFamily="@font/poppinsmedium"

                            tools:targetApi="n" />
                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="10dp"
                android:orientation="vertical"
                app:cardCornerRadius="4dp"
                app:cardElevation="2dp"
                tools:targetApi="n">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:orientation="vertical"
                    android:background="#ffffff"
                    android:padding="8dp"


                    android:weightSum="2">


                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:fontFamily="@font/poppinsmedium"
                        android:gravity="start"
                        android:layout_marginTop="8dp"
                        android:text="@string/delivery_details"
                        android:textAlignment="textStart"
                        android:textColor="#000000"
                        android:textSize="14sp"
                        tools:targetApi="n" />


                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0.4dp"
                        android:layout_marginTop="10dp"
                        android:background="#808080">

                    </View>



                    <TextView
                        android:id="@+id/dm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginEnd="10dp"
                        android:layout_marginStart="10dp"
                        android:fontFamily="@font/poppinsmedium"
                        android:text="@string/personal_shopper"
                        android:textColor="#58595d"
                        android:textSize="14sp"
                        android:layout_marginTop="4dp"
                        tools:targetApi="n"
                        tools:ignore="RtlHardcoded" />



                    <LinearLayout
                        android:id="@+id/tohide"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

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

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:layout_marginEnd="10dp"
                                android:layout_marginStart="10dp"
                                android:fontFamily="@font/poppinsmedium"
                                android:text="@string/rating_selectedn"
                                android:textColor="#58595d"
                                android:textSize="14sp"
                                tools:targetApi="n"
                                tools:ignore="RtlHardcoded" />

                            <TextView
                                android:id="@+id/psrate"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="20dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="2dp"
                                android:layout_marginStart="8dp"
                                android:layout_weight="1"
                                android:fontFamily="@font/poppinsmedium"
                                android:paddingEnd="5dp"
                                android:paddingStart="5dp"
                                android:text="@string/one"
                                android:textColor="#000000"
                                android:textSize="14sp"
                                tools:targetApi="n" />
                        </LinearLayout>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="2dp"

                            android:orientation="horizontal">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="10dp"
                                android:layout_marginLeft="10dp"
                                android:layout_marginStart="10dp"
                                android:fontFamily="@font/poppinsmedium"
                                android:text="@string/at_time"
                                android:textColor="#58595d"
                                android:textSize="14sp"
                                tools:ignore="RtlHardcoded"
                                tools:targetApi="n" />

                            <TextView
                                android:id="@+id/pstime"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="20dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="20dp"
                                android:layout_marginStart="2dp"
                                android:layout_weight="1"
                                android:fontFamily="@font/poppinsmedium"
                                android:paddingEnd="5dp"
                                android:paddingStart="5dp"
                                android:text="@string/_0_00_pm"
                                android:textColor="#000000"
                                android:textSize="14sp"
                                tools:targetApi="n" />
                        </LinearLayout>

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

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:layout_marginEnd="10dp"
                                android:layout_marginStart="10dp"
                                android:fontFamily="@font/poppinsmedium"
                                android:text="@string/delivery_charges"
                                android:textColor="#58595d"
                                android:textSize="14sp"
                                tools:targetApi="n"
                                tools:ignore="RtlHardcoded" />

                            <TextView
                                android:id="@+id/cost"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="20dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="8dp"
                                android:layout_marginStart="2dp"
                                android:layout_weight="1"
                                android:fontFamily="@font/poppinsmedium"
                                android:paddingEnd="5dp"
                                android:paddingStart="5dp"
                                android:text="@string/_100"
                                android:textColor="#000000"
                                android:textSize="14sp"
                                tools:targetApi="n" />
                        </LinearLayout>


                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>

        <TextView
                android:id="@+id/deyapay"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:fontFamily="@font/questrial"
                android:textColor="#000000"
                android:textSize="20sp" />

    <ListView
        android:id="@+id/list"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="16dp"
        />

      </LinearLayout>

    </ScrollView>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:padding="@dimen/activity_horizontal_margin"
        app:cardCornerRadius="4dp"
        app:cardElevation="2dp"
        tools:targetApi="n">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:layout_marginStart="8dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Grand total"
                    android:textColor="#58b90c"
                    android:background="#ffffff"
                    android:textSize="14sp"
                    android:fontFamily="@font/poppinsmedium"
                    />

                <TextView
                    android:id="@+id/amountpaid1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="$00.00"
                    android:textColor="#000000"
                    android:background="#ffffff"
                    android:textSize="14sp"
                    android:fontFamily="@font/poppinsmedium"


                    />
            </LinearLayout>


            <Button
                android:id="@+id/homepage"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1.2"
                android:background="@drawable/buttonbackground"
                android:fontFamily="@font/questrial"
                android:gravity="center_vertical"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:text="Continue"
                android:textAlignment="center"
                android:textColor="#ffffff"
                android:textSize="16sp"
                android:layout_alignParentBottom="true"
                android:textStyle="bold"
                tools:ignore="RtlCompat"
                tools:targetApi="n">

            </Button>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

Thanks in advance

1 Answers

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.

UPDATE:

Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View and ViewGroup classes which provide this functionality. 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).

If you want to make nested scrolling work on pre-Lollipop devices, which you probably do, you have to use corresponding utility classes from the Support library. 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.

Related