我尝试使用工具:listitem在RecyclerView上显示正确样式的样本数据.这在使用原始视图时工作正常.
但是当扩展RecyclerView以创建我自己的自定义视图以添加一些功能时,它只会在Android Studio的布局预览中显示一个空白区域.
扩展时如何使用View的所有布局预览功能?
我的视图如下所示:
class TestRecyclerView(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int
) : RecyclerView(context, attrs, defStyle) { }
有任何想法吗?
解决方法:
您只需在Android Studio中构建或重建项目,您的自定义视图就会显示示例数据.
经过测试,确实有效!我的Android Studio是Android 3.3 Beta 3
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"/>
不要忘记在自定义RecyclerView中放置app:layoutManager属性!
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"