2

i have a layout whose starting tag(parent tag is ) is

<scroll view  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

later i have many images , textView , Edit text and other things, and finally a list view. i have defined the hight of listview as 230dp as prescribed to me. the thing is the whole layout is scrolling nice, but the content inside the listview is not scrolling individually . this is how the xml looks...

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     > 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical" >
    <TextView android:layout_width="fill_parent"
         android:layout_height="50dp"
         android:text="BISCOOT"
         android:textSize="25dp"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="200dp" android:background="#ff000000">

      <ImageSwitcher android:id="@+id/switcher1" android:layout_width="fill_parent" 
      android:layout_height="200dp"  
      android:layout_alignParentLeft="true" android:layout_alignParentRight="true" >
      </ImageSwitcher> 
        <ProgressBar
              android:id="@+id/progressBar2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"  android:layout_centerHorizontal="true"
               />
        <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" /> 

        <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
      android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" /> 
  </RelativeLayout>
    <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Latest headLines"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
         <LinearLayout android:orientation="vertical"
             android:layout_width="fill_parent"            
             android:layout_height="fill_parent">
      <ListView 
        android:id="@android:id/list" 
        android:layout_width="fill_parent" 
        android:scrollbarFadeDuration="0" 
        android:scrollbarStyle="outsideOverlay" 
        android:smoothScrollbar="true" 
          android:layout_height="260dp">
     </ListView>
    </LinearLayout>



    <!-- 
    <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Heroin On the Rocks"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="200dp" android:background="#ff000000">

      <ImageSwitcher android:id="@+id/switcher2" android:layout_width="fill_parent" 
      android:layout_height="200dp"  
      android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> 

        <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" android:src="@android:drawable/ic_media_ff" /> 

        <ImageButton android:id="@+id/imageButton4" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
      android:layout_alignTop="@+id/imageButton1" android:src="@android:drawable/ic_media_rew" /> 
  </RelativeLayout>
  -->




<TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Watch Movies"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp" android:orientation="horizontal"
    android:weightSum="6"
    >
        <ImageView 

            android:layout_height="100dp"
            android:layout_width="wrap_content" android:layout_weight="1"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg1"
            />
         <ImageView android:layout_height="100dp"
            android:layout_width="wrap_content" android:layout_weight="1"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg2"/>
          <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg3"/>

          <ProgressBar
              android:id="@+id/progressBar3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center" />

           <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg4"/>
            <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg5"/>
             <ImageView android:layout_height="100dp" android:layout_weight="1"
            android:layout_width="wrap_content"
           android:layout_marginLeft="2dp" android:layout_marginRight="2dp"
            android:id="@+id/watchmoviesimg6"/>


    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="200dp" 
    android:orientation="vertical"
    >
              <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Photo Stories"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>

              <ProgressBar
                  android:id="@+id/progressBar4"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center" />

               <ImageView 
                   android:id="@+id/photostoryimageview1"
                   android:layout_height="200dp" android:layout_weight="1"
            android:layout_width="fill_parent"
            />
               </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="200dp" 
    android:orientation="vertical"
    >
                 <TextView android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:text="Star Special"
         android:background="@drawable/item_bg_light"
         android:textColor="@android:color/white"
         android:gravity="center"/>

                 <ProgressBar
                     android:id="@+id/progressBar5"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:layout_gravity="center"/>

               <ImageView 
                   android:id="@+id/starSpecialimageView1"
                   android:layout_height="200dp" android:layout_weight="1"
            android:layout_width="fill_parent"
            />
               </LinearLayout>

</LinearLayout>
</ScrollView>
6
  • I think it's not a good idea to put Listview inside of a ScrollView
    – slezadav
    Sep 5, 2012 at 7:06
  • a list view has in built scrolling why are you putting a scrol view on top of it Sep 5, 2012 at 7:07
  • becouse my layout is long , the whole xml should have a parent scrolll , and the listview should have an individual scroll inside Sep 5, 2012 at 7:09
  • But, it seems your xml contains only ListView itself. Then, why it needs to be inside of a ScrollView do you have any more contents in your XML? I mean, posted xml code if full part. Or you just posted relevant code only? Sep 5, 2012 at 7:11
  • 1
    You dont able to do ScrollViw and ListView in one layout. If you have put ScrollView then ListView will not work and if you dont put ScrollView then ListView will work to scroll. Sep 5, 2012 at 11:00

5 Answers 5

6

Simply, remove the ScrollView from your layout. And, make the LinearLayout as parent. And, try to run your app. It will scroll the list.

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.

Have a look at this

Update

Take one parent layout with two child. And, in one layout it should contain ScrollView with your contents. And, on another layout contain your ListView Try like this He has done like that one.

2
  • i tried as you say , but its not working , instead the whole layout keeps scrooling Sep 5, 2012 at 7:11
  • thanx for your support butwith such huge xml , inside parent giving linear layout , then scroll view , then again a linear layout .. giving a force close Sep 5, 2012 at 7:55
1

You dont able to do ScrollViw and ListView in one layout. If you have put ScrollView then ListView will not work and if you dont put ScrollView then ListView will work to scroll.

UPDATE

Even if the listview inside scrollview or scrollview inside listview, both will work. You need to handled its touch accordingly.

1

Try below class, it may help you to work with ListView inside ScrollView

Utility.java

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

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

            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                 View listItem = listAdapter.getView(i, null, listView);
                 if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 }
                 listItem.measure(0, 0);
                 totalHeight += listItem.getMeasuredHeight();
            }

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

Finally use this line just after setting adapter to your listview - Utility.setListViewHeightBasedOnChildren(your_listview);

0

You cannot add a ListView in a scroll View, as list view also scrolls and there would be a synchonization problem between listview scroll and scroll view scroll. You can make a CustomList View and add this method into it.

@Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
    /*
     * Prevent parent controls from stealing our events once we've gotten a touch down
     */
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        ViewParent p = getParent();
        if (p != null) {
            p.requestDisallowInterceptTouchEvent(true);
        }
    }
    return false;
}
0

I found one more solution to get rid of this scrolling problem. Actually using listview in scrollview sometimes might create problems.

We could remove this scrollview and just take one listview. Now create three layout first main layout with listview (which we want to show above list), second for header and third for footer (which we are to show below list) and finally add header and footer in list. This will add header and footer as a list item.

Note: This solution is for small list like 2 or 3 items in list.

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.