I know that ListView
objects shouldn't be put into a ScrollView
. However, I can't come up with a proper approach to this situation: I have two layouts, both including a ListView
, one directly below the other. I want all elements of both lists to be output; naturally, that should take more than the screen can offer, so I want to make the result scrollable, so, as I thought, I should put both into a ScrollView
, but this doesn't work well with ListView
(they only display one item each) and is considered a bad practice. How do I go about this?
Here's a mock up of what I mean:
Two lists, and the entirety of the second list can't fit into the screen, so I'd like to scroll it, and when I do, the second header ('Example2') scrolls up and doesn't stay in place, as opposed to the default behaviour of having the second list content scrolled.
What I have now is:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/example1_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/example1_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/example1_header" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example2_header />
<ListView
android:id="@+id/example2_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Maybe you can use NonscrollListView custom class that extends ListView.
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(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();
}
}
After that you can use it, inside your Scrollview. For example (or you can put another Widget on Scrollview):
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.unicode.teslibrary.NonScrollListView
android:id="@+id/bankList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.unicode.teslibrary.NonScrollListView>
<com.unicode.teslibrary.NonScrollListView
android:id="@+id/bankSecondList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.unicode.teslibrary.NonScrollListView>
</LinearLayout>
</ScrollView>