0

I am using two different ExpandableListViews in my fragment inside a scroll view, one right below the other.

The problem is that only one ExpandableListView heading is displayed when the activity is called. Please refer the image below:

enter image description here

Also, when I click the expandable list view, the list view expands and the other ExpandableListView also displays. Refer the image below:

enter image description here

I want the both the Expandable list views to display when the activity gets called for the first time.

This is my xml:

<ExpandableListView
      android:id="@+id/exLInTheMoodFor"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:scrollbars="none"
      android:groupIndicator="@null"
      android:layout_below="@+id/lblInTheMoodFor"
      android:layout_marginTop="10dp"/>

And this is the java code for defining and initializing the Expandable list view and setting height:

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View convertView = inflater.inflate(R.layout.cafes_more_fragment, container, false);

        final ExpandListChild1 items = new ExpandListChild1();

        exLInTheMoodFor = (ExpandableListView) convertView.findViewById(R.id.exLInTheMoodFor);
        ExpListItems = SetStandardGroups();
        ExpAdapter = new ExpandListAdapterProduct(activity, ExpListItems);
        exLInTheMoodFor.setAdapter(ExpAdapter);

        exLInTheMoodFor.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                setListViewHeight(exLInTheMoodFor, i);
                return false;
            }
        });


        return convertView;
    }

    //for set height show method in expnadable list view
    private void setListViewHeight(ExpandableListView listView, int group) {
        android.widget.ExpandableListAdapter listAdapter = (android.widget.ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            totalHeight += groupItem.getMeasuredHeight();

            if (((listView.isGroupExpanded(i)) && (i != group))
                    || ((!listView.isGroupExpanded(i)) && (i == group))) {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                    totalHeight += listItem.getMeasuredHeight();
                }
            }
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sarthak Sharma
  • 137
  • 3
  • 10

2 Answers2

0

Expanding all groups in the list

for(int i=0; i < myAdapter.getGroupCount(); i++)
mexpandableListView.expandGroup(i);

if Expanding one item in the list

mexpandableListView.expandGroup(0);

One item Expanding and balance item compressed

mexpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                    @Override
                    public void onGroupExpand(int i) {
                        ExpandableAdapter myAdapter= (ExpandableHomeworkAdapter) mexpandableListView.getExpandableListAdapter();
                        if (myAdapter== null){
                            return;
                        }
                        for (int k=0;k<myAdapter.getGroupCount();k++){
                            if (k!=i){
                                mexpandableListView.collapseGroup(k);
                            }
                        }
                    }
                });
0

The problem was using listview/expandablelistview inside scrollview. Generally this combination causes the visibility issue of listview existing inside the scrollview. Full explanation can be found from this answer. This link and this link should also be helpful for solving your problem. I hope this helps.

Imtiaz Abir
  • 731
  • 7
  • 12