Challenge: Use the blank view when crimes is zero


#1

How to do it? Need I to create a layout resource to replace the RecyclerView or Update the fragment_crime_list layout resource.


#2

What I did was change the /layout/fragment_crime_list.xml

It turns out that if a FrameLayout has multiple children, those children will be drawn on top of one another.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout     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">

    <!-- Layer 1 -->
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/crime_recycler_view"/>

    <!-- Layer 2 -->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/empty_list_layout">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/empty_list_text"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginEnd="16dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="parent" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/add_crime_button"
            android:text="@string/new_crime"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="parent" />
    </android.support.constraint.ConstraintLayout>

</FrameLayout>

I put an ID on the ConstraintLayout because I’ll be able to set its visibility to View.INVISIBLE when the list is not empty. The child Views will inherit this visibility change.

Now all of the changes you need to make are in CrimeListFragment.java


#3

Hi,
Can you please show me your code of CrimeListFragment to show empty view when Recyclerview is empty
Thanks in advance


#4

Thanks @tkimia, I go this to work using your approach. I originally tried to add it as a fragment to the end of the RecyclerView but decided in the end this was a crazy approach.

If anyone has any comments on the way I implemented it in CrimeListFragment, please let me know. I’m not sure if I’ve put code in the natural places.

First in onCreateView (just before the updateUI(); statement):

        mEmptyView = (ConstraintLayout) view.findViewById(R.id.empty_layout);

        mNewCrimeButton = (Button) view.findViewById(R.id.new_crime_button);
        mNewCrimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCrime();
            }
        });

Then I extracted an addCrime() method from onOptionsItemSelected(MenuItem item) method:

    private void addCrime() {
        Crime crime = new Crime();
        CrimeLab.get(getActivity()).addCrime(crime);
        Intent intent = CrimePagerActivity.newIntent(getActivity(), crime.getId());
        startActivity(intent);
    }

Then updateUI() has the following added before call to updateSubtitle();:

        mEmptyView.setVisibility(View.VISIBLE);
        if (crimes.size() > 0 ) {
            mEmptyView.setVisibility(View.INVISIBLE);
        }

This seemed to work ok, and when all added crimes were deleted, the message and button became visible again.


#5

Thanks, this helped me.


#6

this was really helpful here is what my empty layout looks like