18

I have a RecyclerView which displays dynamic content. I would to display this RecyclerView in between other Views. For example, one View displayed above and one View displayed below the RecyclerView:

View
RecyclerView
View

However, whatever I try doesn't seem to be working and the RecyclerView seems to take up the entire space. I believe the problem lies in the wrap_content issue, though I've tried some suggested solutions, such as this custom LinearLayoutManager, yet it doesn't seem to fix my problem.

Attempt:

<?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>

Question: Is there a way to place and display a View above and a View below a RecyclerView? If so, how? Or is it better to use a ListView?

chRyNaN
  • 3,592
  • 5
  • 46
  • 74

3 Answers3

35

What about the usage of 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>
saulmm
  • 2,398
  • 15
  • 21
  • Either because my layout is slightly more complex than in the example I gave or because of the known bug with RecyclerView, simply adding a layout_weight didn't work. However, I decided to use a ListView instead of a RecyclerView, and adding layout_weight to the ListView did give me the desired effect. – chRyNaN Oct 13 '15 at 03:30
1

This might not be your typical solution, but you can try it nonetheless.

How about using a RelativeLayout instead of LinearLayout, and simply setting the top and bottom padding of your SwipeRefreshLayout to the height of your views as follows.

<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>

Ari
  • 3,086
  • 1
  • 19
  • 27
  • 1
    Unfortunately this isn't a viable solution for me since my top and bottom views change size dynamically. – chRyNaN Oct 11 '15 at 21:47
  • Why not reset padding also dynamically? – Ari Oct 11 '15 at 22:03
  • Messing with the padding dynamically didn't work for me. I resulted to using a ListView instead and added the layout_weight attribute on it, which produced my desired effect. Thanks, for your help. – chRyNaN Oct 13 '15 at 03:32
1

use header and footer for the RecyclerView , use viewtype in Recyclerview Adapter and add header and footer view.The problem you are facing is because of multiple scollview in one layout and best solution is by using header and footer, this will make it as single scrollview. see this link for more information https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type?rq=1

Community
  • 1
  • 1
Spiker
  • 522
  • 4
  • 21