Два RecyclerViews друг под другом в одном макете
Как я могу получить два RecyclerViews друг под другом в одном макете? Я не хочу иметь один RecyclerView для всех элементов.
Мой код:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/main__item_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:text="@string/find_friends__already_playing"
android:background="@color/header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="@string/find_friends__invite_friends"
android:background="@color/find_friends__header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
Ответы
Ответ 1
Я сам нашел ответ.
Вам нужно поместить LinearLayout в ScrollView и использовать wrap_content
как RecyclerView layout_height
.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/header"
android:gravity="center"
android:text="@string/find_friends__already_playing"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/find_friends__header"
android:gravity="center"
android:text="@string/find_friends__invite_friends" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</LinearLayout>
</ScrollView>
Также есть ошибка с RecyclerView и wrap_content
, поэтому вам нужно использовать менеджер компоновки. Отметьте это сообщение: Как заставить WRAP_CONTENT работать с RecyclerView
Ответ 2
У меня также была такая же проблема, и я написал библиотеку, которая помогает достичь этого, объединив адаптеры и макеты.
Gradle зависимость попробовать (требуется jcenter repo):
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
Измените xml на использование одного RecyclerView, который соответствует родительскому:
<android.support.v7.widget.RecyclerView
android:id="@+id/joined_friends_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
Затем инициализируйте RecyclerView в коде следующим образом:
//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));
//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());
Вы можете проверить эту ссылку для получения дополнительных сведений о библиотеке и объяснения. Надеюсь, что это поможет.
Ответ 3
Вы можете дать каждой высоте RecycleView
равной 0dp и весу равному 1:
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
Ответ 4
Используйте NestedScrollView
как родительский макет, он должен иметь
android:weightSum="2"
и дайте
android:layout_weight="1"
каждому RecyclerView вашего. Он должен прокручиваться один за другим.
Ответ 5
если вы получите нижний recyclerview, не прокручивая основной контент, измените LinearLayout (см. ответ от alan_derua) на ConstraintLayout и оберните два RecyclerView внутри. См. Следующий код:
<ScrollView 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="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/first_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:gravity="left"
android:paddingTop="0dp"
android:text="@string/my_tasks"
app:layout_constraintBottom_toTopOf="@+id/second_list_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/first_list_view" />
<android.support.v7.widget.RecyclerView
android:id="@+id/second_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Это сработало для меня!
Ответ 6
Просто используйте:
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
android:layout_below="@+id/your_first_recycler"/>
последняя строка для вашей проблемы. Используйте ее.