4

Please don't mark as duplicate this question without understanding.

I have gone through all answers which are available but none is able to resolve my issue.

Scenario: I have to add Sections with feature of expand and collapse on click, these sections are dynamic as per API response. So I have taken On xml file with below tree structure.

main.xml

LinearLayout->ScrollView->LinearLayout

Now I am adding custom xml design file in this linear layout as per response, also this response contains number of questions in each section. So for managing questions, I have taken listview in custom xml file. Now I have to show each listview in full height so only top scroll should work no any kind of scroll inside section.

I have checked some answers with common method name setListViewHeightBasedOnChildren but it is not working as listview is added run time dynamically.

So please help me for this.

user2807083
  • 2,962
  • 4
  • 29
  • 37
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • *the common method name setListViewHeightBasedOnChildren* ... hehe, it is terrible, it makes a [very expensive LinearLayout from ListView](https://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing#comments-3495908) [Romain Guy](https://stackoverflow.com/users/298575/romain-guy) - engineer at Google (who is responsible for ListView) – Selvin Jan 17 '18 at 12:08
  • I am not saying that, but in stack overflow when I search I only get this kind of solution with same name in each answer – Vickyexpert Jan 17 '18 at 12:10
  • You may check if NestedScrollView would help ... if not **there is no right solution for puting one ScrollView inside another ScrollView** – Selvin Jan 17 '18 at 12:12
  • I have already checked that but here values are bound run time from web API and same time it is adding views in parent view so it is not able to count actual height of each view – Vickyexpert Jan 17 '18 at 12:14
  • Even I have checked with relative or linear layout instead of list view but not succeed.. So please don't down vote question like this – Vickyexpert Jan 17 '18 at 12:15
  • hey Vickyexpert, So when you click on an Item, It Should Expand to the Height of the ListView Inside of that Item??? And, Is there any other views inside your Items besides the ListView??? – Shahin Mar 02 '18 at 10:23
  • @Shahin, It is little bit complex as suppose I have 25 sections, so all will not cover in mobile screen, now it may be possible each section has 1, 5, 10, 100 data. So when user will call API for data at that time I have to show all data within appropriate section. So I can add dynamic listview with section and section data but I have to make it as expand and collapse. So one will be expand while rest will be collapse. – Vickyexpert Mar 02 '18 at 11:04
  • Vickyexpert Do you want me to write an Expand and Collapse Animation Code for a Single Item with a few Listviews in it?? But, This means that the Height of The ListViews Must become a Specific Value, So You would Need to put Them Inside a ScrollView As Well. – Shahin Mar 02 '18 at 11:09
  • This is Obviously not Optimum. But Worth a Try. Can You Show me your Layout for a Single Item as Well?? – Shahin Mar 02 '18 at 11:11
  • is your listview inside scroll view? is you have countable listview or listview number generated dynamic based on api response? is per listview content come from different api? sry still not get what you expecting.. – Iqbal Rizky Mar 03 '18 at 09:13
  • So you have an X amount of cells inside an A list, which, when clicked, expand and show a B list, collapsing all the other cells? – Fco P. Mar 06 '18 at 18:01
  • i have also used same scenario LL-SV-LL-ListView. mine is single listview where as your is expandable. just give a try to the solution which i gave in below link https://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android/47920002#47920002 – Tara Mar 08 '18 at 07:22

2 Answers2

1

As the number of sub lists are dynamic, you better go through the library

You just need to add one more adapter to maintain sublists. its pretty easy to customize as well.

https://github.com/luizgrp/SectionedRecyclerViewAdapter

Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
1

I have done this, try this:

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

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
Sagar
  • 554
  • 5
  • 21