Java Android - SwipeRefreshLayout ListView hides the last result

34 Views Asked by At

why my last row is hidden in listview? Hello, why my last row is hidden in listview? Hello, why my last row is hidden in listview?

I have changed the xml code 1000 times already and still can't fix it.

SwipeRefreshLayout work good.

enter image description here

fragment_list.xml `

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/refreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="@android:color/transparent"
            android:listSelector="@android:color/transparent"
            android:divider="@null"
            android:dividerHeight="0dp">
        </ListView>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/empty"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/empty_list_text" />
</RelativeLayout>

`

list_row.xml

`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:rippleColor="@android:color/transparent"
        card_view:cardElevation="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:strokeWidth="0dp"
        app:cardElevation="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="194dp"
                app:srcCompat="@drawable/image_list"
                app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.SelectedCornerRadius"
                android:scaleType="centerCrop" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="16dp">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/city"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?attr/textAppearanceHeadline6" />
                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/region"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:textAppearance="?attr/textAppearanceBody2" />
                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="2"
                    android:layout_marginTop="12dp"
                    android:textAppearance="?attr/textAppearanceBody2" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:layout_gravity="center"
                android:orientation="horizontal">
                <com.google.android.material.button.MaterialButton
                    android:id="@+id/btn_check_region"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="20dp"
                    android:text="@string/check_list" />
                <com.google.android.material.button.MaterialButton
                    android:id="@+id/btn_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/check_delete" />
            </LinearLayout>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

</RelativeLayout>

`

I have no idea how to fix it -.-

1

There are 1 best solutions below

0
Crispert On

The ListView's height is MATCH_PARENT which constrains it to the height of its parent ViewGroup. You should always define the height of scrollable views like ListView as WRAP_CONTENT so that it can freely expand according to the number of children. Also it might be a good idea for the parent LinearLayout to be anchored to the top of the RelativeLayout: android:layout_alignParentTop="true". Leaving views floating freely in RelativeLayouts may have surprising consequences.