Reputation: 1186
I have a fragment with an expandable list view which should be scrollable if the content is larger than the remaining screen. However, I cannot find a way to get the scrolling working properly. Here's the fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/titleView"
android:textColor="@color/textColor"
android:paddingHorizontal="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="8pt"
tools:layout_editor_absoluteY="9dp" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/nestedScrollView"
android:fillViewport="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleView"
app:layout_constraintBottom_toBottomOf="parent"
>
<ExpandableListView
android:id="@+id/expListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="8dp"
/>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to wrap a LinearLayout around the NestedScrollView (because some other post said that), but it did not work. When I open an element at the bottom of the list it scrolls down to show the complete child in the view, but the view does not respond to any scrolling attempts, neither by sliding up/down on the items nor clicking on the scroll bar.
Adapter code taken 1:1 from here: http://developine.com/expandable-listview-android-kotlin/ only xml is changed as shown above.
Any ideas?
Upvotes: 0
Views: 1316
Reputation: 1186
I found an answer to my question which solves the problem but as pointed out by @Mwasz this should not be the way you do it. So I document it here just for reference and maybe if somebody has really a reason to use the NestedScrollView
In the ExpandableListView
add
android:nestedScrollingEnabled="true"
to enable the scrolling inside the NestedScrollView.
Upvotes: 2
Reputation: 186
I don't understand why is ExpandableListView wrapped in the NestedScrollView. If you don't have an exact reason for this - remove NestedScrollView
If you need to keep the NestedScrollView - I think Your list isn't scrolling, because the ScrollView consumes touch events. You could try to make a custom ExpendableListView that disallows to intercept the touch event.
Kotlin (if you're using java then change it to java code):
class MyOwnExpendableListView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ExpandableListView(context, attrs) {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
parent.requestDisallowInterceptTouchEvent(true)
return super.dispatchTouchEvent(ev)
}
}
Then use it in your xml:
<MyOwnExpandableListView
android:id="@+id/expListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="8dp"
/>
Upvotes: 1