Виды выше и ниже RecyclerView
У меня есть RecyclerView, который отображает динамический контент. Я бы отобразил этот RecyclerView между другими представлениями. Например, один вид, показанный выше, и один вид, отображаемый под RecyclerView:
View
RecyclerView
View
Однако, что бы я ни пытался, похоже, не работает, и RecyclerView, кажется, занимает все пространство. Я считаю, что проблема заключается в проблеме wrap_content, хотя я пробовал некоторые предлагаемые решения, такие как этот пользовательский LinearLayoutManager, но это, похоже, не устраняет мою проблему.
Попытка
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- AppBar works fine; no issue here -->
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/app_bar_layout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"/>
</android.support.design.widget.AppBarLayout>
<!-- Not displaying; Tried different views -->
<com.chrynan.taginput.view.TextInputWrapper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_container">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text"/>
</com.chrynan.taginput.view.TextInputWrapper>
<!-- Takes up entire screen space -->
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipe_refresh">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- Not displaying -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Вопрос: Есть ли способ разместить и отобразить представление выше и представление ниже RecyclerView? Если да, то как? Или лучше использовать ListView?
Ответы
Ответ 1
Как насчет использования weight
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Ответ 2
Это может быть не ваше типичное решение, но вы можете попробовать его тем не менее.
Как насчет использования RelativeLayout вместо LinearLayout и просто устанавливайте верхнее и нижнее дополнение вашего SwipeRefreshLayout на высоту ваших представлений следующим образом.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- your app bar -->
<!-- Not displaying; Tried different views -->
<com.chrynan.taginput.view.TextInputWrapper
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:id="@+id/edit_text_container"/>
<!-- Takes up entire screen space -->
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="250dp"
android:id="@+id/swipe_refresh">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- bottom view -->
<TextView
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/text_view"/>
</RelativeLayout>
Ответ 3
используйте верхний и нижний колонтитулы для RecyclerView, используйте тип вида в адаптере Recyclerview и добавьте представление заголовка и нижнего колонтитула. Проблема, с которой вы сталкиваетесь, связана с множеством scollview в одном макете, а лучшим решением является использование верхнего и нижнего колонтитула, это сделает его как одиночный scrollview. см. эту ссылку для получения дополнительной информации /info/12061/how-to-create-recyclerview-with-multiple-view-type