12

I try to use tools:listitem to show correctly styled sample-data on a RecyclerView. This works fine when using the original view.

But when extending RecyclerView to create my own custom view to add some functionality it just shows an empty space in the layout-preview in Android Studio.

How can I use all of the layout-preview stuff of a View when extending it?

My View looks like this:

class TestRecyclerView(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int
) : RecyclerView(context, attrs, defStyle) { }

Any ideas?

hardysim
  • 2,756
  • 2
  • 25
  • 52
  • The preview seems to work for other default-views, e.g. TextView (a custom-EditText renders just as a normal one). Maybe it's related to the implementation of the Design time attributes? E.g. they check for `RecyclerView` and not for `T extends RecyclerView`? – hardysim Nov 08 '18 at 09:55

2 Answers2

17
<com.test.my.TestRecyclerView
        android:id="@+id/your_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/your_item"/>

Don't forget to put a app:layoutManager attribute in your custom RecyclerView!

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

enter image description here

Vince
  • 2,551
  • 1
  • 17
  • 22
  • 1
    No sry, this does not work. When not built, the layout-preview shows a gray area with the name of the View in it. When built, it shows a white / empty area. As said, it seems to work for a simple extension of TextView (and maybe others) but not for `tools:listitem` on a extended `RecyclerView`. Or did this really work for you with my `TestRecyclerView`? – hardysim Nov 08 '18 at 12:35
  • I am not able to get this working. I've created a blank demo-project (https://github.com/hardysim/custom-RecyclerView) to illustrate it. I've tested with Android Studio 3.2 stable and 3.3 Beta 3. Please have a look. – hardysim Nov 09 '18 at 08:43
  • Ok !'m going to try it – Vince Nov 09 '18 at 08:47
  • 3
    I founded your problem. You have to put a `app:layoutManager` in your recyclerview ✔ (Updated post) – Vince Nov 09 '18 at 09:28
  • That's it. Good catch! Thx. – hardysim Nov 09 '18 at 12:15
0

AndroidStudio listitem

implementation 'androidx.recyclerview:recyclerview:1.1.0'

item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:orientation="vertical"
    tools:background="@tools:sample/backgrounds/scenic">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        tools:text="@tools:sample/first_names" />

</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    tools:listitem="@layout/item"
...
    >

[Read more here]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205