Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make empty view take up remaining space after RecyclerView?

I'm trying to make an empty view that is colored take up the remaining space after a RecyclerView. I've looked at different StackOverflow solutions but nothing works. Essentially I want something like this :

________________________
|                       |
|                       |
|                       |
|    Recycler view      |
|                       |
|  -------------------  |
|    colored empty view |
|                       |
|                       |
|                       |
|                       |
|                       |
_________________________

It should shrink as the RecyclerView grows. 

________________________
|                       |
|                       |
|                       |
|    Recycler view      |
|                       |
|                       |
|                       |
|                       |
|                       |
|  -------------------  |
|    colored empty view |
|                       |
|                       |
_________________________

However if the RecyclerView exceeds one screen's worth, there should be no empty view. Here's what I have so far:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/bla_bla_layout">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/bla_bla_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <View
        android:background="@color/grayBackground"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:contentDescription="@string/add_bla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/add_bla_icon"
        fab:fab_colorNormal="@color/primary"
        fab:fab_colorPressed="@color/primary_pressed"
        fab:fab_colorRipple="@color/ripple" />

</FrameLayout>

EDIT This is not a duplicate of the mentioned question and you could see this if you read both questions. The solution to my question should be purely xml, layout based. By "empty view" I mean just some colored rectangle BELOW the existing RecyclerView, not a textview that actually says "Empty view" when there's no data in the RecylerView.

like image 234
Tariq Avatar asked Aug 13 '15 19:08

Tariq


Video Answer


1 Answers

Recycler view calculate its height at runtime and we uses LinearLayoutManager to specify its height and width.

But problem is that, right now LinearLayoutManager does not support wrap_content it uses always match_parent for it's height.

So you have to use this LinearLayoutManager not android.support.v7.widget.LinearLayoutManager.

copy above java class in your package and use it like below,

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
// if it has fixed size
recyclerView.setHasFixedSize(true);

I tested it and its working as you need.

like image 147
Vishal Makasana Avatar answered Oct 20 '22 11:10

Vishal Makasana