Challenge Solution: an Empty View for a RecyclerView


#1

Hi!
To complete this challenge I’ve done the following steps:

  • Updated the fragment_crime_list.xml layout’s view hierarchy (add TextView & Button)
  • In updateUI() method wrote a logic to make a new View (with text & button) visible when there’re no crimes in the list
  • Updated CrimeListFragment.java file by adding & initializing 2 member variables for fragment_crime_list.xml new FrameLayout and Button
  • Set onClickListener on Button so that a new Crime could be created

fragment_crime_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/crime_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    android:id="@+id/add_crime_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="There are no crimes yet. Do you have anything to report?"
    android:gravity="center_horizontal"
    android:textSize="20dp"
    android:textStyle="italic" />

    <Button
        android:id="@+id/add_crime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="16dp"
        android:text="@string/add_crime_button"
        android:textAlignment="center"
        android:textStyle="bold"
        android:elegantTextHeight="true"
        android:theme="@style/Base.AlertDialog.AppCompat.Light"/>
</LinearLayout>

CrimeListFragment.java

public class CrimeListFragment extends Fragment {

private View mLayout;
private Button mAddCrimeButton;

 @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
        mCrimeRecyclerView=(RecyclerView) view.findViewById(R.id.crime_recycler_view);
        mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    mLayout = view.findViewById(R.id.add_crime_view);
    mAddCrimeButton = view.findViewById(R.id.add_crime);
    mAddCrimeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Crime crime = new Crime(); //creating new Crime object
            CrimeLab.get(getActivity()).addCrimes(crime); //adding new Crime obj to CrimeLab
            Intent intent = CrimePageActivity.newIntent(getActivity(),crime.getId());
            startActivity(intent); //starting an instance of CrimePageActivity to edit new Crime

        }
    });

    if (savedInstanceState != null){
        mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
    }
    return view;
}

private void updateUI(){

    CrimeLab crimeLab = CrimeLab.get(getActivity());
    List<Crime> crimes = crimeLab.getCrimes();

    mLayout.setVisibility((crimes.size() > 0? View.GONE : View.VISIBLE));

.....
}