0

I am using ListView in ScrollView and fetching ListView total height to making Scrollview height equal to ListView complete height. I am using below code for getting ListView total height:

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

But problem is that I have variable height ListView Items, when one of TextView in ListView Item got content of 3-4 lines, height of ListView Item goes large and above method not providing exact height of that item. Because of that, not able to scroll to the end of ListView.

So please give me some idea how to get corrent height of ListView With variable size items.

mark
  • 503
  • 5
  • 27
  • Why are you using a listview inside a scrollview? –  Mar 27 '15 at 12:14
  • @ jvrodrigues I have some reasons to use this, if you know the solution of my issue, please help me. thank you – mark Mar 27 '15 at 12:17
  • 1
    see this question I used it in my one of the project. http://stackoverflow.com/questions/18813296/non-scrollable-listview-inside-scrollview/24629341#24629341 – Shabbir Dhangot Mar 27 '15 at 12:22
  • @Shabbir Dhangot - Thanks a lot, that solved my issue. You got my actual problem. If you can, please make it as answer so that I can mark it as correct. – mark Mar 27 '15 at 13:02

1 Answers1

0

For Use Listview inside scroll view you need to get height of of the listview and then add to the scrollview.

this below link will help and perfect solution of your problem.

Answer of Hiren Dedaniya

Community
  • 1
  • 1
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80