Currently, after the Criminalintent app launches, a blank list is displayed. From the user experience, even if the crime list is empty, you should show hints or explain class information. Set an empty view to show a message similar to "No crime record can be displayed." Add a button to allow users to create new crime records directly. Determines whether the crime list contains data, and then uses the Setvisibility method of any class to control the display of the placeholder view.
First modify the Fragment_crime_list view to add a TextView and a button control:
<relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ><Android.support.v7.widget.RecyclerView Android:id= "@+id/crime_recycler_view"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"/><TextView Android:id= "@+id/crime_set_empty_text_view"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"android:textsize= "20SP"Android:text= "No data displayed"/><Button Android:id= "@+id/click"android:gravity= "Center"Android:text= "Create New Crime"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/></relativelayout>
Then set the global variables for the TextView and Button in the Crimelistfragment
Private TextView Mtextview; Private Button Mclick;
The two variables are then initialized in the Oncreateview () method and a Click event is set on the button, where the click event is the same as the Click event for the Menu menu item
1Mtextview =(TextView) View.findviewbyid (R.id.crime_set_empty_text_view);2Mclick =(Button) View.findviewbyid (R.id.click);3Mclick.setonclicklistener (NewView.onclicklistener () {4 @Override5 Public voidOnClick (View v) {6Crime Crime =NewCrime ();7 Crimelab.get (Getactivity ()). Addcrime (crime);8Intent Intent =crimepageractivity.newintent (Getactivity (), Crime.getid ());9 startactivity (intent);Ten } One});
After that, add the following code to the UpdateUI () method
1 if (Crimes.size () > 0) {2 mtextview.setvisibility (view.gone); 3 mclick.setvisibility (view.gone); 4 } Else {5 mtextview.setvisibility (view.visible); 6 mclick.setvisibility (view.visible); 7 }
The meaning of the code is that when the crimes collection is not empty, Mtextview and Mclick are hidden, otherwise they are displayed.
Android Authority programming Challenge Exercise 13.8 empty view for Recyclerview