user3534519
user3534519

Reputation: 99

Making ListView part of ScrollView

I have a layout where I have multiple components in a ScrollView. I want to include a List within the ScrollView. The problem is if I include a ListView within the scroll view, the ListView shows in a small space and the list scrolls too as the list adds items dynamically. If I add the ListView out of the ScrollView It displays nicely but I dont want the list to scroll and be a part of the whole page

Here is my code:-

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10" >

        <ScrollView
            android:id="@+id/scrollNews"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="4" >

            <!-- Main  vertical LinearLayout -->

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

                <TextView
                    android:id="@+id/txtNewsTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:padding="10dp"
                    android:text="Large Text"
                    android:textAlignment="center"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <!-- Layout for Date, Likes, comments and views -->

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

                    <LinearLayout
                        android:layout_width="98dp"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_corner" >

                        <TextView
                            android:id="@+id/txtPubDate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="#F8F8F8"
                            android:textSize="11sp" />
                    </LinearLayout>

                    <ImageButton
                        android:id="@+id/btnViews"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginLeft="20dp"
                        android:background="@color/white"
                        android:src="@drawable/btnviewpressed" />

                    <TextView
                        android:id="@+id/txtNewsViews"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="view"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#FF3C6FA6" />

                    <ImageButton
                        android:id="@+id/btnComments"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:background="@color/white"
                        android:src="@drawable/btncommentpressed" />

                    <TextView
                        android:id="@+id/txtNewsComments"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="view"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#FF3C6FA6" />
                </LinearLayout>

                <ImageView
                    android:id="@+id/imgNewsImage"
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/attini" />

                <TextView
                    android:id="@+id/txtNewsBody"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <ImageView
                        android:id="@+id/btnComms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:background="@color/white"
                        android:src="@drawable/commspressed" />

                    <ImageButton
                        android:id="@+id/btnAddComms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        android:src="@drawable/btnaddcommspressedxml"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>
        </ScrollView>

        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/getdata"
            android:layout_gravity="bottom"
            android:layout_weight="6"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector"
            android:padding="5dp"
            android:transcriptMode="alwaysScroll"
            android:visibility="invisible" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Views: 195

Answers (1)

tasomaniac
tasomaniac

Reputation: 10342

There are hacks that enables ListView in a ScrollView but they are not good.

  • You should either have only one ListView with header and footer views. You should add these header and footers programatically.
  • Or you can have a ScrollView and a LinearLayout instead of ListView and you can add the items in the list one by one to the LinearLayout programatically.

If the list is the main thing and it is big then the first approach is better.

Upvotes: 1

Related Questions