Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded
Asked Answered
Q

5

25

I have an ExpandableListView inside a NestedScrollView (yes I know, it is not good to have a scrolling view inside another scrolling view but I don't know what else to do, please do tell me if anybody knows a better approach).

The size of the content in NestedScrollView is still within the screen so it won't scroll, but when ExpandableListView is expanded, the content will leak outside the screen but the NestedScrollView still won't scroll.. Why is this so?

Here's my NestedScrollView layout :

<NestedScrollView>
    <LinearLayout>
        <LinearLayout></LinearLayout>
        ... // About 3 of the LinearLayouts
        <ExpandableListView/>
    </LinearLayout>
</NestedScrollView>
Qualm answered 3/6, 2016 at 3:40 Comment(1)
refer this link thedeveloperworldisyours.com/android/…Interactive
F
84

You can use NonScrollExpandableListView you can achieve non-scroll property of any Lisview or GridView or ExpandableListView by overriding following method.

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
} 

So for using NonScrollExpandableListView you need to make one custom class.

public class NonScrollExpandableListView extends ExpandableListView {

    public NonScrollExpandableListView(Context context) {
        super(context);
    }

    public NonScrollExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollExpandableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

And use it like.

<com.example.extraclasses.NonScrollExpandableListView 

    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> 

Happy coding.

Forsythia answered 3/6, 2016 at 4:23 Comment(18)
Will this cause performance issue?? It worked btw but I'm concerned about the performance issue (had NestedScrollview parenting RecyclerView and oh damn, the memory usage.......)Qualm
Not at all. infect it is recommended to use this in cases like yours.Forsythia
It worked well, but can I just use expandableListView.onMeasure() and not make new class? I am still new at making those kind of custom classes.. Oh and btw it is "in fact" not "infect" lolQualm
hahah.. I'm bad at spellings.. That is me.. anyway.. you have to make separate class for that.. you can't use it that ways..Forsythia
it is override method of view class. you can use it only if you are extending any view. and if you see I have done coding inside onMeasure(). so if you use it like you mentioned it will take default scroll. and won't give desire result.Forsythia
how to setselectedgroup in expandablelistview??? As it is not scrolling when the group is expanded.Anubis
@RishabhSrivastava can you elaborate your problem more?Forsythia
@vrundpurohit suppose there are 10 parent groups. And I want click on 8th, so I want the expandablelistview to scroll and show all its child which is not happening.Otherwise this solution is awesome.Anubis
have you tried warping NonScrollExpandableListView inside ScrollView?Forsythia
if you did that you probably able to do that. are you using ScrollView or 'NestedScrollView'?Forsythia
Thanks Worked For me. will it work with ListView and Recycler View Too?Shag
Yes for ListVIew and i don't Recommend for RecyclerViewForsythia
How can I animate view while it expand using NonScrollExpandableListView?Lyssa
thanks a lot! But in my case I have a coordinatorlayout and inside it is my nestedscrollview and inside that is my expandablelistview so, what's happening is that my expandablelistview isn't expanding to the fullest unless the keyboard opens, which is weirdCesaro
@V-rundPuro-hit How to do the same for ListView instead of ExpandableListView??Please help?Rotarian
@V-rundPuro-hit for me it worked only after i changed Integer.MAX_VALUE >> 2 to Integer.MAX_VALUE >> 21 . Why its so? Also it work's with ScrollView not with NestedScrollView.Rotarian
@V-rundPuro-hit Seems like this will not work correctly for 3 level or 4 level ExpandableListview. Any help ??Rotarian
Tried this solution to add expandable list view and recycler view to nested scrollview. Works fine, to be able to scroll both lists as one, but when you tap on a expandable list view group, when child expands, it doesn't scroll to the child content.Procreant
V
8

Add android:nestedScrollingEnabled="true" to your ExpandalbleListView layout.

Vinosity answered 22/8, 2017 at 22:14 Comment(0)
P
6

The answer from V-rund Puro-hit is what worked for me. But it took some modifications to work with Kotlin supporting API >19. So for the purpose of saving someone time, here it is:

Create a new class file NonScrollExpandableListView:

import android.content.Context
import android.util.AttributeSet
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView


class NonScrollExpandableListView : ExpandableListView {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun setAdapter(adapter: ExpandableListAdapter?) {
        super.setAdapter(adapter)
    }

    override fun setOnChildClickListener(onChildClickListener: OnChildClickListener) {
        super.setOnChildClickListener(onChildClickListener)
    }

    override fun expandGroup(groupPos: Int) : Boolean {
        return super.expandGroup(groupPos)
    }

    override fun expandGroup(groupPos: Int, animate: Boolean) : Boolean {
        return super.expandGroup(groupPos, animate)
    }

    override fun isGroupExpanded(groupPosition: Int): Boolean {
        return super.isGroupExpanded(groupPosition)
    }

    public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom)
        val params = layoutParams
        params.height = measuredHeight
    }
}

...and I use it like so:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <com.example.you.kotlinlistview.NonScrollExpandableListView
                android:id="@+id/expandableCategories"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <Button
                android:id="@+id/add_new_feed"
                android:drawableStart="@drawable/ic_add"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Add new feed"
                />

            <Button
                android:id="@+id/settings_button"
                android:drawableStart="@drawable/ic_settings"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Settings"
                />

            <Button
                android:id="@+id/logout_button"
                android:drawableStart="@drawable/ic_exit"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Log Out"
                />

        </LinearLayout>
    </ScrollView>

As a result, I can comfortably add buttons and other elements to NavigationView side drawer) and it all works nicely in one common ScrollView. Main usage here is when you need to combine multiple ListViews and ExpandableListViews inside the one common ScrollView which would take care of scrolling.

Puke answered 6/7, 2019 at 7:43 Comment(11)
How to do the same for ListView instead of ExpandableListView??Please help?Rotarian
@Rotarian In the exact same way, you just need to override ListView class, and you don't need to override all the expand... classes. You can post a new question if you think it's beneficial to have the code for exactly that case (if you do, don't forget to refer to this question).Puke
ok thank you.Can u explain what does this means in ur code Integer.MAX_VALUE shr 2 ?? what here 2 stands for??Rotarian
for me it worked only after i changed Integer.MAX_VALUE >> 2 to Integer.MAX_VALUE >> 21 . Why its so? Also it work's with ScrollView not with NestedScrollView.Rotarian
@Rotarian In Kotlin shr is the keyword for shift right operation. Regarding the MeasureSpec it is good to start with docs. But generally this allows the expandable list view to change height depending on its state (expanded groups), instead of having a fixed height and becoming scrollable.Puke
when i make it to 21 or 22 list is scrolling otherwise not scrolling...Rotarian
It should not be scrolling. That was the whole point. You want to make it non-scrolling so you can combine it with other views. Then you make the whole thing scrollable by putting all these views inside a one common ScrollView.Puke
Seems like this will not work correctly for 3 level or 4 level ExpandableListview. Any help ??Rotarian
Scrolling is not smooth.Nephew
@HirenPatel I think the smoothness of scrolling is something one solves with profiling. If the drawing of the list elements takes a significant amount of computation it causes skipped frames. This is an independent issue, different from grouping the elements in a single srollable view. Or is this solution causing a significant computation overhead for you?Puke
super.onMeasure is throwing exceptionBald
I
3

Use this method this will calculate the ExpendableListSize at run time.

 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();

            }
        }
    }

    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();

}

An call this method in your setOnGroupClickListener.like below

mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            setListViewHeight(parent, groupPosition);
            return false;
        }
    });
Itinerary answered 13/10, 2017 at 9:23 Comment(0)
T
0

We can't use listview, gridview or expandable listview inside scrollview. If you wan't to use expandable listview inside scrollview then you have to give some fixed height to your expandable listview.

Thwart answered 3/6, 2016 at 4:6 Comment(4)
Oh I see, so it has to be a fixed height.. If I give a fixed height, for example 40 dp, would it take that much space even if it is expanded? And how do people make ExpandableListView inside another scrolling view? I've seen several apps like that..Qualm
@KevinMurvie By default android won't fully support one scrollable view inside another scrollable view. But we can do something and it can be done. But it will not be a perfect one. For your referenceThwart
Actually I've seen that and I've applied that (RecyclerView inside NestedScrollView).. But too bad it has performance issues and I do it with recyclerview.. So those apps actually doesn't play by the rules?Qualm
Exactly, we can do something and we can achieve that type of scrolling but it will have performance and scrolling issues.Thwart

© 2022 - 2024 — McMap. All rights reserved.