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.
@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.
ListView not expanding inside NestedScrollView
For the CoordinatorLayout
to work properly you need the scrolling child to implement NestedScrollingChild. Such classes are NestedScrollView
and RecyclerView
.
To say it short - just use a RecyclerView
for your scrolling content and it'll work correctly :)
P.S. As a side note, I don't see a reason why you'd use a ListView
anymore. I know it's a habit and it's easier to setup (because you've done it many times), but using a RecyclerView
is the recommended way anyways.
Scroll View inside xml Contains 2 expandablelistview doesn't scroll
Try using NestedScrollView instead of ScrollView. Although it's always recommended that you should use a single ListView or RecyclerView with different view types.
NestedScrollView is just like ScrollView, but it supports acting as
both a nested scrolling parent and child on both new and old versions
of Android. Nested scrolling is enabled by default.
Android: Scroll whole fragment with ListView inside
Alright, after combining multiple answers, I have the solution that I needed.
First
I needed to use a NestedScrollView
instead of a regular ScrollView
.
It solves the conflict between the two scroll containers (ScrollView
and ListView
).
Reference: Android: ScrollView vs NestedScrollView
NOTE: My list content is dynamic, so it can be too short to fill the remaining space. I had to set android:fillViewport="true"
on the NestedScrollView
. If the list is longer than the remaining space, it will not cause any trouble.
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/card1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- NOTE: constraints properties are missing from here for easier reading -->
<!-- card content here -->
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- NOTE: constraints properties are missing from here for easier reading -->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- NOTE: this will change in step 3 -->
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Second
Following the steps above will make the ListView
collapse to the height of its first item. To solve this, I needed to create a subclass from ListView
and override its onMeasure()
method, so it can calculate the proper height at runtime.
Reference: Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded
NonScrollListView.java
package my.package.name
import ...
public class NonScrollListView extends ListView {
// NOTE: right click -> create constructors matching super
@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();
}
}
Third
I needed to use my custom View instead of the regular ListView
in my layout XML.
layout.xml excerpt
<my.package.name.NonScrollListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
This way I managed to make it work. Both cards move together on scroll, even if I tap the ListView
area.
I don't know whether it causes performance issues with really long lists, because mine contains a few dozen items at most, but I have no problem on a low end Galaxy A20e.
Related Topics
How to Read Text File in Android
Creating Different Layout for Android Phone and Tablet
Compatiblity of Material Design to Versions Below Android 5.0
How to Get Email Id from Facebook Sdk in Android Applications
Android Ble Bluetoothgatt.Writedescriptor() Return Sometimes False
How to Deal with Different Aspect Ratios in Libgdx
On Showing Dialog I Get "Can Not Perform This Action After Onsaveinstancestate"
Android Download PDF from Url Then Open It with a PDF Reader
Tiled Drawable Sometimes Stretches
Android: Get Facebook Friends List
Handle a View Visibility Change Without Overriding the View