0

Iam new to Android, iam developing an application with listview in a scrollview. According to my requirement i need to show some of views with vertical scrolling and below that a listview. I want to scroll the entire view and listview vertically, where as listview must scroll independently. I have googled for the solution but found that we should not use Listview in a scrollview.

I have tried An alternative to this, by replacing the Listview with the Dynamic LinearLayouts, that is also not working.

I tried doing the disabling the focus of the ScrollView when I touch the ListItem and viceversa, but i didn't get it.

I tried the below code to disable the focus of scroll view.

mScrollView.setFocusableInTouchMode(true);
mScrollView.setFocusable(true);

mScrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

mScrollView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.requestFocusFromTouch();
        return false;
    }
});

Please help me out, thanks in advance

dave.c
  • 10,910
  • 5
  • 39
  • 62
Bhaskar
  • 23
  • 4
  • 6

3 Answers3

2

ListViews are not meant to be used inside a scrollview.

For your ref: see this

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

do not nest ListView inside a ScrollView

if you have to do so, try the code below, it's also from StackOverflow answered by one guy.

public class Utility {
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);
}
}
Community
  • 1
  • 1
Longerian
  • 723
  • 5
  • 13
0

As you got to know from others..

ListView inside ScrollView Won't Work.

For the shake for solution,

You can add the other layout part...your views into one xml and listview in another

Then use listView.setHeaderView(view); to add your view layout as header of that.

Example Code :

LinearLayout ll = (LinearLayout) findViewById(R.id.headerlayout); ListView lv = (ListView)findViewById(R.id.listview); lv.addHeaderView(ll);

see this or you can also search on internet for Example for Listview with Header

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98