-3

I am developing an application where i have to show the listview in scrollview and the problem is that i am not getting the proper height of listview i have followed almost all the code from below links.

Android ListView rows in ScrollView not fully displayed - clipped

How can I put a ListView into a ScrollView without it collapsing?

But every time i am getting listview like this enter image description here

I don't know how should I calculate the height of list I tried no of different ways but due to variable size of each row in list it is not working. If any one knows how it can be calculated it run time in Adapter file or from main activity/fragment let me know how should do it.

Please help me and thanks in advance.

Community
  • 1
  • 1
Crash
  • 51
  • 6
  • 2
    Listiview itself has scrollview – Amit Vaghela Feb 20 '16 at 11:03
  • 1
    It's not a good idea to put a scrolling view inside another scrolling view. – Elvis Chweya Feb 20 '16 at 11:05
  • http://stackoverflow.com/questions/35325256/scrollview-only-works-in-listview/35402052#35402052 – mitesh viradiya Feb 20 '16 at 11:05
  • go through the above link. its use full to you because I think your problem solution is there – mitesh viradiya Feb 20 '16 at 11:08
  • i have already used this code but still I am facing the above issue. is there any way that i can do it via custom. if possible let me know but I have never made custom control in android. – Crash Feb 20 '16 at 11:11
  • 1
    It maybe simpler to solve your problem. Why put the `ListView` in a `ScrollView`? What is your end? – Elvis Chweya Feb 20 '16 at 11:19
  • because i have to show the order in listview and below the list i have to show the charges and all other detail so i have to take the scrollview and listview both. i can not show the whole screen here. so you may not get it that how my screen will look – Crash Feb 20 '16 at 11:21

1 Answers1

0

Simple solution. Add this line setListViewHeightBasedOnChildren(item_Listview); and the method like below

if(item_lists != null && item_lists.size() > 0) {
                itl = new Itemlist(getActivity(), item_lists);
                item_Listview.setAdapter(itl);
                setListViewHeightBasedOnChildren(item_Listview);
            }



    private void setListViewHeightBasedOnChildren(ListView item_listview) {
            ListAdapter listAdapter = item_listview.getAdapter();
            if (listAdapter == null) {
                return;
            }
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, item_listview);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = item_listview.getLayoutParams();
            params.height = totalHeight
                    + (item_listview.getDividerHeight() * (listAdapter.getCount() - 1));
            item_listview.setLayoutParams(params);
        }
Dev Tamil
  • 629
  • 1
  • 9
  • 25
  • not working i tried now and i am having the same type of code from number of links one or more line is added/removed to this method other wise whole code in this method is same almost where ever i searched. – Crash Feb 20 '16 at 11:50
  • please help i am working on this issue since 3-4 days – Crash Feb 20 '16 at 11:51
  • i have did same in xml too, but nothing is working for me. i dont know where i am making a mistake. By the way thanks to all for helping me – Crash Feb 20 '16 at 12:01
  • hello Tamil Arasan please accept invitation from Deeplove9493@gmail.com – Crash Feb 22 '16 at 10:20