Nick Nelson
Nick Nelson

Reputation: 1293

ListView UNDER ScrollView

Is it possible to place my list view UNDER my scrollview. I have a page with a bunch of items that don't fit on the screen so I use a scrollview. Then I want a listview to place comments under all of these items.

I have done a hack to measure the listview height and placed it inside the scrollview, but I know this is frowned upon so is there a way I can just place it under the scrollview? Sample code would be great. I have tried placing it under in XML, but it didn't show on the screen. Thanks!

<ScrollView
        android:id="@+id/tabone2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/backrepeat"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/mainimage" >
            </ImageView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/Description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="TITLE"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/tvTrack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dip"
                android:layout_marginTop="10dip"
                android:gravity="center_horizontal"
                android:text="Trackname"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textStyle="bold" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/ratetitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="RATE"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <RatingBar
                android:id="@+id/ratingbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:numStars="5"
                android:paddingBottom="6dp"
                android:paddingTop="10dip"
                android:stepSize="0.5" />

            <Button
                android:id="@+id/ratingbutton"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_marginBottom="10dip"
                android:layout_marginTop="7dip"
                android:background="@drawable/pagebutton"
                android:gravity="center_horizontal|center_vertical"
                android:text="Submit Rating"
                android:textStyle="bold"
                android:textSize="10sp" >
            </Button>

             <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/favorite"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="FAVORITE"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909"
                android:layout_marginBottom="10dip" />
            <RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dip">

            <Button
                android:id="@+id/favoritebutton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"                  
                android:background="@drawable/pagebutton"
                android:gravity="center_horizontal|center_vertical"
                android:text="Add to Favorites"
                android:layout_marginLeft="7dip"
                android:layout_marginRight="7dip"
                android:textStyle="bold"
            >
            </Button>
            <ProgressBar
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/progress_bar5"     
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_gravity="center_horizontal|center_vertical"
    android:indeterminate="true"
    android:visibility="gone" />
            </RelativeLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/Description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="DETAILS"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909"
                android:layout_marginBottom="10dip" />

            <TextView
                android:id="@+id/tvTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:gravity="center_horizontal"
                android:text="Tracktime"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textStyle="bold" >
            </TextView>

            <TextView
                android:id="@+id/tvViews"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" >
            </TextView>

            <TextView
                android:id="@+id/tvRatings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" >
            </TextView>

            <TextView
                android:id="@+id/tvPlays"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" >
            </TextView>

            <TextView
                android:id="@+id/tvDownloads"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginBottom="10dip"
                android:textStyle="bold" >
            </TextView>

            <View
                android:id="@+id/viewdesc1"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/Description2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="DESCRIPTION"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:id="@+id/viewdesc2"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909"
                android:layout_marginBottom="10dip" />

            <TextView
                android:id="@+id/tvDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:layout_marginBottom="10dip"
                android:textAppearance="?android:attr/textAppearanceLarge" >
            </TextView>

            <Button
                android:id="@+id/playbutton1"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="7dip"
                android:layout_marginRight="7dip"
                android:layout_marginTop="7dip"
                android:background="@drawable/pagebutton"
                android:text="PLAY"
                android:textStyle="bold" >
            </Button>

            <Button
                android:id="@+id/downloadbutton3"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="7dip"
                android:layout_marginRight="7dip"
                android:layout_marginTop="7dip"
                android:background="@drawable/pagebutton"
                android:text="DOWNLOAD"
                android:textStyle="bold" >
            </Button>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909" />

            <TextView
                android:id="@+id/tvComments"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:text="COMMENTS"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" >
            </TextView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:background="#b00909"
                android:layout_marginBottom="10dip" />

            <ListView
            android:id="@+id/lvComments"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:cacheColorHint="#00000000"
            android:divider="#00000000"
            android:listSelector="#00000000" />


            <EditText 
              android:id="@+id/etComments"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="5dip"
              android:layout_marginBottom="5dip"
              android:layout_marginLeft="7dip"
              android:layout_marginRight="7dip"
              android:hint="Add comment..."
              android:singleLine="false"/>
            <Button
                android:id="@+id/btnAddComment"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="7dip"
                android:layout_marginRight="7dip"
                android:layout_marginTop="7dip"
                android:background="@drawable/pagebutton"
                android:text="Post Comment"
                android:textStyle="bold" >
            </Button>


        </LinearLayout>
    </ScrollView>

I did not include the ViewFlipper portion...

Upvotes: 1

Views: 1057

Answers (4)

Rajesh Wadhwa
Rajesh Wadhwa

Reputation: 1036

Use this method

 public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter(); 
            if (listAdapter == null) {
                // pre-condition
                return;
            }

            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }

Upvotes: 2

Nick Nelson
Nick Nelson

Reputation: 1293

I solved the problem by placing everything above the list in a list header and everything below the list in a list footer. Thanks for the inputs!

Upvotes: 0

Carl-Emil Kjellstrand
Carl-Emil Kjellstrand

Reputation: 1243

If you really want a listview you could perhaps put the two parts of you layout in a http://developer.android.com/reference/android/widget/SlidingDrawer.html and thus avoiding the problem of showing two scrolling views at the same time. Haven't tried this myself so it's a shot in the dark.

Would suggest the approach Joel mentioned unless you have very large amounts of data in the list.

Upvotes: 0

Joel Martinez
Joel Martinez

Reputation: 47809

What I've done is used the LinearListView from this SO answer:
https://stackoverflow.com/a/5646121/5416

It lets you place the linear list view inside of a scrollview so that you can mix it with other scrollable content that is not part of the list.

Upvotes: 0

Related Questions