12

I have few contents follwed by Expandable listview.I am not able to scroll the entire layout. I have been searching for appropriate answer more than a week..Suggest some answers.

Satty
  • 189
  • 1
  • 1
  • 9
  • if you want to scroll your layout then you can use scroll bar in you layout. – Himani Mar 29 '14 at 12:31
  • https://stackoverflow.com/questions/29943600/how-can-i-put-a-expandablelistview-into-a-scrollview-without-it-collapsing/46660821#46660821 – Ankur Bavishi Oct 10 '17 at 07:40
  • i have solve same problem > https://stackoverflow.com/questions/29943600/how-can-i-put-a-expandablelistview-into-a-scrollview-without-it-collapsing/46660821#46660821 – Ankur Bavishi Oct 10 '17 at 07:41
  • 1
    [I solve this problem by this way](https://stackoverflow.com/questions/29943600/how-can-i-put-a-expandablelistview-into-a-scrollview-without-it-collapsing/46660821#46660821) – Ankur Bavishi Oct 10 '17 at 07:52

9 Answers9

19

This solution worked for me, when i used custom layout inside navigation view having scroll view with LinearLayout which has Expandable listview.

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header">
    <ScrollView
            android:fillViewport="true"
            android:layout_marginTop="130dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/homeLayout"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:background="@drawable/layout_click_effect"
                    android:layout_width="match_parent"
                    android:layout_height="45dp">

                    <ImageView
                        android:id="@+id/homeIv"
                        android:layout_width="45dp"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_home_black_24dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Home" />
                </LinearLayout>
                <View
                    android:background="@android:color/darker_gray"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"/>

                <ExpandableListView
                    android:id="@+id/topCatgExpLv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:groupIndicator="@null"
                    android:dividerHeight="1dp" />
        </ScrollView>


    </android.support.design.widget.NavigationView>

In your onCreate

mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
    MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
            mGroups);
    mListView.setAdapter(adapter);
    mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);
            return false;
        }
    });

private void setListViewHeight(ExpandableListView listView, int group) {
    ExpandableListAdapter listAdapter = (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();

            }
            //Add Divider Height
            totalHeight += listView.getDividerHeight() * (listAdapter.getChildrenCount(i) - 1);
        }
    }
    //Add Divider Height
    totalHeight += listView.getDividerHeight() * (listAdapter.getGroupCount() - 1);

    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();
}
Amit Tumkur
  • 2,752
  • 26
  • 27
  • 1
    Worked for me. Thanks – Usman Rana Jun 28 '16 at 06:57
  • but it works only when i click the groupitem of expandable listview.when i scroll the view its not working – S HemaNandhini Jan 02 '18 at 08:23
  • @SNandhini the above code 'setListViewHeight' adds children height and shows it only when group is clicked, if you want to show children with group expanded u shud do it manually like set group expanded and accordingly call above 'setListViewHeight' method by passing Exp Listview and group position like in a for loop. – Amit Tumkur Jan 08 '18 at 08:20
  • @AmitTumkur It's not working for multilevel ExpandableListview like 3 level or 4 level. Any help?? – KJEjava48 Jul 28 '19 at 13:52
  • @KJEjava48 your top/1st level expandable view height should be calculated depending upon child expandable listviews expanded/closed. – Amit Tumkur Jul 29 '19 at 14:28
8

you can do this very simple . set your height of scroll view to wrap_content. set your height of root view inside scroll view as wrap_content (such as linear layout) and set your expandable list view to wrap_content. after that set OnGroupExpandListener and OnGroupCollapseListener for your expandable list view and set height for that as long as child of expandable list view. like this code : your layout :

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollViewDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/mainGray"
    >
 <ExpandableListView
    android:id="@+id/expandableListCategory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
                />
 </LinearLayout>
 </ScrollView>

and this is your fragment: (or in your activity)

public class DrawerFragment extends Fragment implements, OnGroupExpandListener, OnGroupCollapseListener{
ScrollView scrollView;
ExpandableListView expListView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
view rootView = inflater.inflate(R.layout.fragment_layout, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.scrollViewDrawer);
expListView = (ExpandableListView) rootView.findViewById(R.id.expandableListCategory);
....
}
@Override
public void onGroupExpand(int groupPosition) {
    LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
    param.height = (childCount * expListView.getHeight());
    expListView.setLayoutParams(param);
    expListView.refreshDrawableState();
    scrollView.refreshDrawableState();
}

@Override
public void onGroupCollapse(int groupPosition) {
    LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
    param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
    expListView.setLayoutParams(param);
    expListView.refreshDrawableState();
    scrollView.refreshDrawableState();
}
Saber Solooki
  • 1,182
  • 1
  • 15
  • 34
  • 1
    When calculating param.height in onGroupExpand, I had to add 1 to child count, to account for the fact that when expanded, the height is the header plus the children. (This assumes that the header layout height and child header height are the same size.) – k2col Aug 07 '15 at 13:55
  • hi this code is adding extra space at the botton of expandable list view – pooja majoka Jun 12 '20 at 19:35
  • not working i have tried many methods like include in another xml its not working – Sulman Rasheed Apr 06 '21 at 11:47
3

I know the question is pretty old, but may be it will be helpful for someone. Basically my answer is some what a combination of the answers of Amit Tumkur and user2141833. After a lot of trial and error, the following code is working for me:

First calculating the initial height of the Expandable list view i.e. when the whole thing is collapsed

    for (Integer i = 0; i < mAdapter.getGroupCount(); i++) {
        View groupItem = mAdapter.getGroupView(i, false, null, mExpandableListView);
        groupItem.measure(mExpandableListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        mInitialHeight += groupItem.getMeasuredHeight();
    }

Then when the group is clicked set the height of the Expandable List view to Wrap Content

    mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            //Other Expansion/Collapsing Logic
            setListHeightToWrap();
            return true;
        }
    });

setListHeightToWrap is a different method:

private void setListHeightToWrap() {
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mExpandableListView.getLayoutParams();
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    mExpandableListView.setLayoutParams(params);
    mExpandableListView.refreshDrawableState();
    mScrollView.refreshDrawableState();
}

Then in OnGroupExpandListener set the height of the Expandable List View as:

    mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStitchingWorksListView.getLayoutParams();
            //The Logic here will change as per your requirements and the height of each of the children in the group
            if (mAdapter.getRealChildrenCount(groupPosition) > 6) {
                params.height = 9 * mInitialHeight;
            } else {
                params.height = 6 * mInitialHeight;
            }
            //For Last Group in the list and the number of children were less as compared to other groups
            if (groupPosition == mAdapter.getGroupCount() - 1) {
                params.height = 3 * mInitialHeight;
            }
            mExpandableListView.setLayoutParams(params);
            mExpandableListView.refreshDrawableState();
        }
    });

Also the layout was ExpandableListView inside LinearLayout inside ScrollView.

Hope this helps someone. :)

Swapnil Thube
  • 103
  • 1
  • 5
Varun Ramani
  • 1,389
  • 1
  • 10
  • 14
2

Firstly I suppose you are not using more than one child layout inside a scroll view, as a scroll view can have only one direct child.

Secondly you should not use any scrolling component like expandable list, inside a scroll view here's why ListView inside ScrollView is not scrolling on Android.

Community
  • 1
  • 1
ask_aks
  • 52
  • 1
2

For users who are looking for expandable listview into scrollview, use views instead of expandable list follow the below link & code-

enter image description here

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linear_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</ScrollView>

And In Java Class some thing like this-

for (int i = 0; i < pProductArrayList.size(); i++) {

            LayoutInflater inflater = null;
            inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mLinearView = inflater.inflate(R.layout.row_first, null);

            final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
            final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
            final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
            final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);

            if(isFirstViewClick==false){
            mLinearScrollSecond.setVisibility(View.GONE);
            mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
            }
            else{
                mLinearScrollSecond.setVisibility(View.VISIBLE);
                mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
            }

            mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(isFirstViewClick==false){
                        isFirstViewClick=true;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                        mLinearScrollSecond.setVisibility(View.VISIBLE);

                    }else{
                        isFirstViewClick=false;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                        mLinearScrollSecond.setVisibility(View.GONE);   
                    }
                    return false;
                } 
            });
Manish Srivastava
  • 1,649
  • 2
  • 15
  • 23
  • This method works only if you have under 100 items because on more the application will freez until the inflate for all items is done – MSA Jul 17 '15 at 13:47
0

Enter the code below adapteSet

enter code hereListAdapter listAdapter = listView.getAdapter();
 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);
 listView.requestLayout();
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
vaisakh mv
  • 71
  • 5
0

Plese, try this once.

1) Add this line setExpandableListViewHeight(expandableListViewCategories) right after setting your adapter.

 expandableListCategoriesAdapter = new ExpandableListCategoriesAdapter(getActivity(), listDataHeader, listDataChild); 
 expandableListViewCategories.setAdapter(expandableListCategoriesAdapter);
 expandableListCategoriesAdapter.notifyDataSetChanged();
 setExpandableListViewHeight(expandableListView);

2) Coppy the bellow content and past it in to setExpandableListViewHeight() method.

private void setExpandableListViewHeight(ExpandableListView listView) {
        try {
            ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getGroupCount(); i++) {
                View listItem = listAdapter.getGroupView(i, false, null, listView);
                listItem.measure(0, 0);
                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();
            scrollBody.post(new Runnable() {
                public void run() {           
                    scrollBody.fullScroll(ScrollView.FOCUS_UP);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Note: This will automatically bring down your ExpandableListView and you can scroll with full view.

Surendar D
  • 5,554
  • 4
  • 36
  • 38
0

After trying too many solutions,i had combined two solutions any it's work properly menuHeight is important.

  expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) 
        expandableListView.getLayoutParams();
        param.height = (2 * menuHeight); //adjust 2 according to you
        expandableListView.setLayoutParams(param);
        expandableListView.refreshDrawableState();
        findViewById(R.id.sv_menu).refreshDrawableState();

    }
});

static int menuHeight =0; // declare globally
public static void setListViewHeightBasedOnChildren(ExpandableListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
        View.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, ViewGroup.LayoutParams.WRAP_CONTENT));

    view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();

menuHeight = totalHeight;}
Swapnil Thube
  • 103
  • 1
  • 5
0

Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View and ViewGroup classes which provide this functionality. To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true" to its XML declaration or by explicitly calling setNestedScrollingEnabled(true).

meekash55
  • 307
  • 3
  • 6