我们在布局文件里定义RecyclerView时,可以使用tools属性预览数据,如下:
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" tools:itemCount="10" tools:layoutManager="LinearLayoutManager" tools:listitem="@layout/item_person_info" tools:orientation="vertical" />
item_person_info文件定义如下:
<?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="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" tools:text="@tools:sample/full_names" /> <ImageView android:layout_width="40dp" android:layout_height="40dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" tools:src="@tools:sample/avatars" /> </android.support.constraint.ConstraintLayout>
预览效果如下:
将tools:layoutManager的值改为GridLayoutManager,预览效果如下: