5

When using a recycler view with LinearLayoutManager.HORIZONTAL and height as wrap_content is it trying to keep the list items having the same height? It seems it does and some text is cut off. How can I solve this?
This is the layout for the item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/boundaries"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:layout_marginEnd="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:orientation="vertical"
        android:gravity="center"
        >

        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
            android:gravity="center"
            android:src="@drawable/my_icon”
            />


    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:maxWidth="200sp"
            android:text="Some text of various length"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:includeFontPadding="false"
            android:layout_marginStart="12dp"
            android:layout_marginBottom="16dp"
            android:gravity="bottom"
            android:text="Some text that is not more than a line"
            />
    </LinearLayout>

</LinearLayout>
Jim
  • 3,845
  • 3
  • 22
  • 47
  • Did you set your RecyclerView height as `wrap_content`? – Doctiger Nov 16 '18 at 14:07
  • @Kenny: Yes. If I set a fixed height there it seems to allow items to have different heights but I think it will cut of things if an item has more content than that – Jim Nov 16 '18 at 14:08
  • In order to prevent text cut-off, you just need to set RecyclerView height to `wrap_content` and item view layout height to `wrap_content` both. – Doctiger Nov 16 '18 at 14:12
  • @Kenny: I have. Check xml in my post – Jim Nov 16 '18 at 14:22

1 Answers1

2

RecyclerView keeps same height by default. It uses max height from visible item view layouts as its height at rendering time. So this causes a problem of text cut-off as there might be an item which has large content than visible items.

In my opinion, it's not much needed to create a custom layout manager like this guy did. For performance and usability, I think it's just better to guess the max height of your item view and set it to RecyclerView height as a constant like in dp size.

If you really want to set the correct height, you need to write some java code to estimate actual height of item views. RecyclerView should have its own height and this is determined at rendering time. And contents are dynamic. Therefore you need to write some code to estimate all of your item views and find the max height to set as RecyclerView's height in java code.

Doctiger
  • 2,318
  • 2
  • 15
  • 28
  • Turns out the problem was I had `android:maxWidth="200sp"`. Once I removed that the `wrap_content` worked properly. I had added that because I thought it would restrict the length of the text and not be a very long line but doesn't work as I expected – Jim Nov 17 '18 at 11:23