1

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...

2
  • It is VERY long because I am using a ViewFlipper but okay.. Feb 24, 2012 at 21:11
  • Would adding a headerview work? I am scared to try because it's a lot of code to move and I've never used a listview header Feb 24, 2012 at 21:14

4 Answers 4

2

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);
        }
0

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.

2
  • I already have a listview inside a scrollview working, I am looking to get rid of that because it is frowned upon and i want it to be an endless loading list (comments) Feb 24, 2012 at 21:11
  • But that's my point, it's frowned upon because of the listview's implementation. But LinearListView (source in that other SO answer) just extends a regular LinearLayout, which is perfectly ok to go inside a ScrollView Feb 24, 2012 at 21:15
0

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.

3
  • Do you think I can place all the items above the listview in a header for the listview? Feb 24, 2012 at 22:03
  • That should probably work, might be a bit strange ui but if it solves your problem its all good i guess :D Feb 24, 2012 at 22:56
  • It works great. The layout is fine and everything, the scrollbar on the side just changes sizes depending on which part is being scrolled.. (header, list, footer) Feb 25, 2012 at 3:56
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.