0

I have to generate a dynamic listview in android. The listview is put inside a scrollview because I have other content too to display above the listview. Something like this : enter image description here

Down part is the listview which is dynamically added. This all comes fine on a portrait mode on Samsung note5 but when I change the screen orientation to landscape the listview does not scroll. This could be because I have given wrap-content for the listview.

Please check my layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:background="@color/gray25"
        android:id="@+id/scrollView"
        android:layout_above="@id/footer">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/mainLayout"
            android:background="@color/light_blue"
            android:paddingBottom="@dimen/_10sdp">
        <!--Top header-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_vertical"
                android:id="@+id/topheader"
                android:background="@drawable/dashboard_button"
                android:layout_marginLeft="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_5sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:padding="@dimen/_5sdp">

            </LinearLayout>
        <!--middle portion-->


            <ListView
                android:id="@+id/RFIDList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:transcriptMode="alwaysScroll"
                android:layout_marginTop="@dimen/_10sdp"
                android:background="@drawable/dashboard_button"
                android:layout_marginLeft="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_5sdp"
                android:minHeight="@dimen/_50sdp" />

        </LinearLayout>
    </ScrollView>
<!-- Footer aligned to bottom -->
    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="@color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="left"
            android:layout_centerInParent="true"
            android:layout_marginLeft="@dimen/_15sdp">               
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

I cannot give fixed height to the listview because its items are generated dynamically. I tried to find the solution and also tried changing my listview to recyclerview but did not work. but Any help would be appreciated.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Arti
  • 2,993
  • 11
  • 68
  • 121
  • 1
    [Android list view inside a scroll view](//stackoverflow.com/q/18367522) – Manohar Mar 06 '18 at 06:04
  • Possible duplicate of [Android list view inside a scroll view](https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view) – Reaz Murshed Mar 06 '18 at 06:17

2 Answers2

1

If you use RecyclerView use NestedScrollView instead of ScrollView and set

recyclerView.setNestedScrollingEnabled(false); after setting layout manager

for ListView see my answer over here

Manohar
  • 22,116
  • 9
  • 108
  • 144
1

Use below method after listView.setAdapter(yourAdapter); as setlistViewHeight(listView,context);

Your problem will be solved.

public static void setlistViewHeight(ListView listView, Context context) {

    ListAdapter myListAdapter = listView.getAdapter();

    if (myListAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {

        View listItem = myListAdapter.getView(size, null, listView);
        if (listItem instanceof ViewGroup)
            listItem.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int screenWidth = display.getWidth();
        int listViewWidth = screenWidth - 65;
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth,
                View.MeasureSpec.AT_MOST);
        listItem.measure(widthSpec, 0);

        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (myListAdapter.getCount()));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
Zia
  • 1,204
  • 1
  • 11
  • 25