0

I have and XML file like this (problem is with RecyclerView - scroll to the end of the file):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recipe_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Scroll View Layout -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <!-- Main Layout-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true">

            <!-- Recipe Image -->
            <ImageView
                android:id="@+id/recipe_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:adjustViewBounds="true"
                android:background="@color/background_3"
                android:scaleType="centerCrop"
                android:src="@drawable/no_image_placeholder"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <!-- Toolbar -->
            <include
                android:id="@+id/toolbar"
                layout="@layout/transparent_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <!-- Recipe Title And Recipe Info -->
            <LinearLayout
                android:id="@+id/layout_title_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/background_8"
                android:layout_below="@id/recipe_image">

                <!-- Recipe Title -->
                <TextView
                    android:id="@+id/recipe_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white_text"
                    android:fontFamily="sans-serif-medium"
                    android:textSize="22sp"
                    android:background="@color/background_8"
                    android:paddingTop="48dp"
                    android:paddingBottom="8dp"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:text="Szarlotka z nadzieniem kakaowym" />

                <!-- Recipe Info -->
                <TableLayout
                    android:id="@+id/table_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:shrinkColumns="*"
                    android:stretchColumns="*"
                    android:padding="8dp">

                    <!-- Row 1 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <!-- Column 1 [Kitchen/Meal Type] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_script"
                                    android:layout_marginRight="2dp"/>

                                <!-- Kitchen/Meal Type Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_kitchen_meal_type"/>

                            </LinearLayout>

                            <!-- Kitchen/Meal Type -->
                            <TextView
                                android:id="@+id/recipe_category"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:textColor="@color/white_text"
                                android:fontFamily="sans-serif-light"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [Difficulty Level] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_marginLeft="8dp"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_signal"
                                    android:layout_marginRight="2dp"/>

                                <!-- Difficulty Level Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_difficulty_lvl"/>

                            </LinearLayout>

                            <!-- Difficulty Level -->
                            <TextView
                                android:id="@+id/recipe_difficulty_lvl"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:fontFamily="sans-serif-light"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                    <!-- Row 2 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp">

                        <!-- Column 1 [Preparation Time] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_timer"
                                    android:layout_marginRight="2dp"/>

                                <!-- Preparation Time Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_preparation_time"/>

                            </LinearLayout>

                            <!-- Preparation Time -->
                            <TextView
                                android:id="@+id/recipe_preparation_time"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [People Count] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_marginLeft="8dp"
                            android:layout_weight="1"
                            android:layout_column="1">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_human"
                                    android:layout_marginRight="2dp"/>

                                <!-- People Count Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_people_count"/>

                            </LinearLayout>

                            <!-- People Count -->
                            <TextView
                                android:id="@+id/recipe_people_count"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                    <!-- Row 3 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp">

                        <!-- Column 1 [Add Date] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_calendar"
                                    android:layout_marginRight="2dp"/>

                                <!-- Add Date Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_add_date"/>

                            </LinearLayout>

                            <!-- Add Date -->
                            <TextView
                                android:id="@+id/recipe_add_date"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:layout_marginLeft="20dp"
                                android:layout_marginTop="2dp"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [Author Name] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_marginLeft="8dp"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_telegram"
                                    android:layout_marginRight="2dp"/>

                                <!-- Author Name Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_proposition"/>

                            </LinearLayout>

                            <!-- Author Name -->
                            <TextView
                                android:id="@+id/recipe_author_name"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="17sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                </TableLayout>

            </LinearLayout>

            <!-- Floating Like Button With Counter -->
            <LinearLayout
                android:id="@+id/like_counter_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                android:padding="8dp"
                android:layout_marginBottom="-53dp"
                android:layout_alignBottom="@id/recipe_image"
                android:layout_alignParentEnd="true">

                <!-- Like Floating Button -->
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/recipe_like"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:src="@drawable/ic_action_like" />

                <!-- Like Counter -->
                <TextView
                    android:id="@+id/recipe_counter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:textSize="12sp"
                    android:textColor="@color/white_text"
                    android:layout_alignBottom="@id/recipe_like"
                    android:text="0"/>

            </LinearLayout>

            <!-- Add To Favourites Floating Button -->
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/recipe_add_to_favourites"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:layout_marginBottom="-28dp"
                android:src="@drawable/ic_action_add_to_favourites"
                android:layout_toLeftOf="@id/like_counter_layout"
                android:layout_alignBottom="@id/recipe_image"/>

            <!-- Recipe Description -->
            <LinearLayout
                android:id="@+id/layout_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/background_9"
                android:padding="8dp"
                android:layout_below="@id/layout_title_info">

                <!-- Recipe Description Label-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="4dp"
                    android:fontFamily="sans-serif-medium"
                    android:textColor="@color/white_text"
                    android:textSize="22sp"
                    android:text="@string/recipe_description" />

                <!-- Recipe Description -->
                <TextView
                    android:id="@+id/recipe_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:textSize="17sp"
                    android:fontFamily="sans-serif-light"
                    android:textColor="@color/white_text"
                    android:text="-"/>

            </LinearLayout>

            <!-- Comments -->
            <LinearLayout
                android:id="@+id/comment_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:background="@color/background_2"
                android:layout_below="@id/layout_description">

                <!-- Comments Label-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginBottom="8dp"
                    android:fontFamily="sans-serif-medium"
                    android:textColor="@color/primary_text"
                    android:textSize="22sp"
                    android:text="@string/recipe_comments" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp">

                    <!-- New Comment Input -->
                    <EditText
                        android:id="@+id/recipe_new_comment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:layout_weight="1"
                        android:hint="@string/hint_new_comment" />

                    <!-- Comment Button -->
                    <ImageButton
                        android:id="@+id/recipe_submit_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="4dp"
                        android:background="@color/background_2"
                        android:tint="@color/icon_1"
                        android:src="@drawable/ic_add_comment"/>

                </LinearLayout>

                <!-- Recycler View -->
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recipe_recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"/>

                <!-- More Comments Button -->
                <android.support.v7.widget.AppCompatButton
                    android:id="@+id/recipe_more_comments"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:stateListAnimator="@null"
                    android:theme="@style/AppTheme.ButtonStyle4"
                    android:padding="17dp"
                    android:text="@string/btn_more_comments"/>

            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

</android.support.design.widget.CoordinatorLayout>

I am displaying max 5 elements in RecyclerView and I would like it to set it's height depending on number of elements that have been added inside it. For example when I inserted 5 elements I would like it to display 3 elements - rest coudl be scrolled down. If I download only 1 I woould like to display only that 1 without scrolling. When RecyclerView will not get any elemetns I would like to set it' height to 0 so it would not be visibile. Any ideas how to achieve this? I must set height manulayy to show anything. When no items are inserted RecyclerView shows blank, empty space which I want to avoid. Any ideas? I also tried to remove RecyclerView and try to insert views to separate LinearLayout but that didn't work. Here is my post about it but noone helped me :/ (Adding custom View to LinearLayout dynamicly does not work)

Community
  • 1
  • 1
anton86993
  • 638
  • 1
  • 9
  • 26

1 Answers1

0

I suggest you to create a custom RecyclerView:

public class CustomRecyclerView extends RecyclerView {

    final int ITEM_HEIGHT = 72;

    public CustomRecyclerView(Context context) {
        super(context);
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int itemCount = getAdapter().getItemCount();
        if (itemCount <= 3) {
            super.onMeasure(widthSpec, ITEM_HEIGHT * itemCount);
        } else if (itemCount < 5) {
            super.onMeasure(widthSpec, ITEM_HEIGHT * 3);
        } else {
            super.onMeasure(widthSpec, ITEM_HEIGHT * 5);
        }
    }
}
iamkaan
  • 1,495
  • 2
  • 23
  • 43
  • I get and error: `android.view.InflateException: Binary XML file line #505: Error inflating class`. Created custom RecyclerView in a package and used it like that: ` ` I have alsho changed it in my Activity. – anton86993 Jan 02 '16 at 21:35
  • Oh sorry, looks like there should be one more constructor: http://stackoverflow.com/a/9661176/2121682. I fixed to code, it should now work. – iamkaan Jan 02 '16 at 23:52