简体   繁体   English

android.support.v7.widget.RecyclerView作为根视图

[英]android.support.v7.widget.RecyclerView as the root view

Can the android.support.v7.widget.RecyclerView be the root view in a layout file or I must add it as a child of "RelativeLayout" element? android.support.v7.widget.RecyclerView可以作为布局文件中的根视图吗,还是必须将其添加为“ RelativeLayout”元素的子级?

Layout A 布局A

  <android.support.v7.widget.RecyclerView
      android:id="@+id/itemsRecyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layoutManager="android.support.v7.widget.GridLayoutManager"
      app:spanCount="2"/>

Layout B 版面B

<RelativeLayout 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="match_parent"
                tools:context=".ui.activity.MainActivity">

  <android.support.v7.widget.RecyclerView
      android:id="@+id/itemsRecyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layoutManager="android.support.v7.widget.GridLayoutManager"
      app:spanCount="2"/>

</RelativeLayout>

As official doc says: 如官方文档所述:

The root element can be either a ViewGroup, a View, or a element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown. 根元素可以是ViewGroup,View或一个元素,但是必须只有一个根元素,并且必须包含xmlns:android属性以及android命名空间,如图所示。

so you could use RecyclerView as the root view: 因此您可以将RecyclerView用作根视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


</android.support.v7.widget.RecyclerView>

Yes you can take recyclview as root element of activity.But it's a good practice to take any view group as a root and recyclview as child view. 是的,您可以将recyclview作为活动的根元素。但是,将任何视图组作为根并将recyclview作为子视图是一个好习惯。

Here is code: 这是代码:

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView没有LayoutManager android.support.v7.widget.RecyclerView - RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView 收到错误“android.view.InflateException:膨胀 class android.support.v7.widget.RecyclerView 时出错” - recieving error “android.view.InflateException: Error inflating class android.support.v7.widget.RecyclerView” 无法导入导入android.support.v7.widget.RecyclerView; - Cannot import import android.support.v7.widget.RecyclerView; &#39;布尔android.support.v7.widget.RecyclerView $ LayoutManager.canScrollVertically()&#39; - 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' 蚂蚁,错误膨胀类android.support.v7.widget.RecyclerView - Ant, Error inflating class android.support.v7.widget.RecyclerView NPE @ android.support.v7.widget.RecyclerView $ LayoutManager.detachViewInternal - NPE @ android.support.v7.widget.RecyclerView$LayoutManager.detachViewInternal android.support.v7.widget.RecyclerView无法实例化 - android.support.v7.widget.RecyclerView failed to instantiate 没有找到类“android.support.v7.widget.RecyclerView - Didn't find class "android.support.v7.widget.RecyclerView android.support.v7.widget.RecyclerView null吗? 使用LinearLayoutManager - android.support.v7.widget.RecyclerView null ? with LinearLayoutManager eclipse中无法启动android.support.v7.widget.Recyclerview错误 - android.support.v7.widget.Recyclerview could not be initiated error in eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM