ExpandableListView inside a NestedScrollView won’t scroll? Use a Non-scrolling ExpandableListView

Using an ExpandableListView inside a NestedScrollView makes it unscrollable, because of the NestedScrollView scrolling interference. To solve this, you can create a non-scrolling ExpandableListView with size equal to its content.

Create this class

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class NoScrollExListView extends ExpandableListView {

    public NoScrollExListView(Context context) {
        super(context);
    }
    public NoScrollExListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NoScrollExListView(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 instead of ExpandableListView, use NoScrollExListView.

<com.your.app.package.NoScrollExListView
    android:id="@+id/exListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</com.admads.notesapp.NoScrollExListView>

 


Also published on Medium.

By |2019-04-05T09:41:26+00:00March 14th, 2019|Categories: Android Tips|0 Comments

Leave A Comment