2

I am having a listview inside a scrollview, but the problem is that the scrollview is scrolling but listview is not scrolling. I think this is due to that scrollView.

Can somebody who has a working solution post it here as reference?

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
  • How could your ScrollView scroll in the first place? – Vikram Gupta Apr 12 '13 at 12:44
  • This not a good practice to do but if you try to give height to listview on runtime than its possible. see below link :- http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing/3495908#3495908 – duggu Jun 24 '13 at 12:17

3 Answers3

7

Generally, you cannot put scrollable things inside other scrollable things, where they scroll in the same direction, and have the results be reliable. Occasionally this works (e.g., WebViews in a ViewPager), but that is the exception, not the norm.

Either:

  • Move the ListView out of the ScrollView, or

  • Move all the rest of the contents of the ScrollView into the ListView, whether using things like addHeaderView() or my MergeAdapter

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
5

ListView must have fixed height as below in your XML file

        <ListView android:id="@+id/lv"
            android:listSelector="#0f0"            
            android:layout_width="fill_parent"
            android:layout_height="500px" />

In Java file, write below code after setContentView()

        lv = (ListView)findViewById(R.id.lv);
        lv.setAdapter(your adapter here);  // you have to add your adapter here             

        lv.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE)
                {
                    lv.scrollBy(0, 1);
                }
                return false;
            }
        });

Make these changes to your code and test it. After too many experiments i written this code. It is working 100% fine.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
5

If you put your ListView/any scrollable View inside the scrollView it will not work properly because when you touch the screen ,main focus of your touch is on parent view(scrollView ) not the child View (ListView).

deepak Sharma
  • 1,641
  • 10
  • 23