android.support.v7.widget.RecyclerView в качестве корневого представления - PullRequest
0 голосов
/ 01 декабря 2018

Может ли android.support.v7.widget.RecyclerView быть корневым представлением в файле макета, или я должен добавить его в качестве дочернего элемента элемента "RelativeLayout"?

Макет 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"/>

Макет 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>

Ответы [ 2 ]

0 голосов
/ 01 декабря 2018

Да, вы можете использовать recyclview в качестве корневого элемента активности. Но хорошей практикой является использование любой группы представлений в качестве корневого и recyclview в качестве дочернего представления.

Вот код:

<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" />
0 голосов
/ 01 декабря 2018

Как сказано в официальном документе:

Корневым элементом может быть ViewGroup, View или элемент, но должен быть только один корневой элемент, и он должен содержать атрибут xmlns: androidс пространством имен Android, как показано.

, чтобы вы могли использовать 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...