Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded
You can use NonScrollExpandableListView
you can achieve non-scroll property of any Lisview
or GridView
or ExpandableListView
by overriding following method.
@Overridepublic 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.
Add android:nestedScrollingEnabled="true"
to your ExpandalbleListView layout.
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.Contextimport android.util.AttributeSetimport android.widget.ExpandableListAdapterimport android.widget.ExpandableListViewclass 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 ListView
s and ExpandableListView
s inside the one common ScrollView
which would take care of scrolling.