Haroon khan
Haroon khan

Reputation: 1058

Recycler view does not show items list when layout_height = "match_constraint" in Constraint layout

I want to create a UI like:this is what I want for which I am using a recycler view under constraint layout. But the issue is when I set layout_height="match_constraint" of recyclerView then it does not show any list and when I set to layout_height="wrap_content" then the list goes out the bound and is not scrollable and when I set it to a fixed length then it does not fill the remaining screen vertically. and it is shown like this: when layout_height = "fixed_size"

Here is how it shows nothing when layout_height = "match_constraint":

layout_height = "match_constraint":

Here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".searchResultListFragment">


    <ImageView
        android:id="@+id/thumbnail_img"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:scaleType="fitXY"
        android:src="@drawable/shopping_mart_transparent"
        app:layout_constraintBottom_toTopOf="@id/search_result_recycler_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_search_result_msg"
        style="@style/search_result_msg_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:text="Attraction Points in Naran"
        app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv_search_result_count"
        style="@style/search_result_count_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="3 Results"
        app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
        app:layout_constraintEnd_toEndOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/search_result_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/thumbnail_img" />
</android.support.constraint.ConstraintLayout>

Upvotes: 1

Views: 1126

Answers (2)

Haroon khan
Haroon khan

Reputation: 1058

As by searching a lot I could not find any solution to this issue. So I came up with my own solution by replacing constraint layout with RelativeLayout. Although this is the ideal solution but this is how I resolved the Issue I was facing.

Here is the xml code:

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="0">
        <ImageView
            android:id="@+id/thumbnail_img"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:scaleType="fitXY"
            android:src="@drawable/shopping_mart_transparent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_search_result_msg"
            style="@style/search_result_msg_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:text="Attraction Points in Naran"
            app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/tv_search_result_count"
            style="@style/search_result_count_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="22dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="3 Results"
            app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
            app:layout_constraintEnd_toEndOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/search_result_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Upvotes: 0

Hocine B
Hocine B

Reputation: 389

It is because at the very last line of your RecyclerView xml your added a wrong character

Change :

app:layout_constraintTop_toBottomOf="@+id/thumbnail_img"

to :

app:layout_constraintTop_toBottomOf="@id/thumbnail_img"

Same for other Views (ImageView and 2 TextViews), adding a + means that you want to add a reference not to link to that reference.

Plus you don't need this line in the ImageView :

app:layout_constraintBottom_toTopOf="@+id/search_result_recycler_view"

Upvotes: 1

Related Questions