Android RecyclerView错误

Tak*_*r07 8 java xml android gradle android-recyclerview

今天我试图使用新的android RecyclerView.我创建了一个带有空白活动的新项目,并在其布局中添加了以下内容:

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

在gradle构建依赖项中,我添加了:

compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.android.support:cardview-v7:21.+'
Run Code Online (Sandbox Code Playgroud)

但activity_main.xml呈现仍然显示以下错误:

Rendering Problems The following classes could not be instantiated:
- android.support.v7.widget.RecyclerView (Open Class, Show Exception)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE  Exception Details java.lang.UnsupportedOperationException: Unsupported Service: accessibility   at com.android.layoutlib.bridge.android.BridgeContext.getSystemService(BridgeContext.java:465
Run Code Online (Sandbox Code Playgroud)

我似乎没有在网络上找到解决方案,因为每个人都说"在你的gradle构建中实现以下依赖项:..."我已经添加了.

有人能提供解决方案吗?

关心Tak3r07

编辑:onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
Run Code Online (Sandbox Code Playgroud)

mt0*_*t0s 7

我认为使用RecyclerView至少可以设置布局管理器.

如果你在这里阅读文档:

与其他适配器支持的视图(如ListView或GridView)相比,RecyclerView允许客户端代码为子视图提供自定义布局安排.这些安排由RecyclerView.LayoutManager控制.必须提供LayoutManager才能使RecyclerView正常运行.

您可以在实例化RecyclerView后尝试设置LayoutManager:

recyclerView.setLayoutManager(new LinearLayoutManager(this));
Run Code Online (Sandbox Code Playgroud)

(后来将其更改为LayoutManager更符合您需求的)