Android Authority programming Challenge Exercise 13.8 empty view for Recyclerview

Source: Internet
Author: User
Tags recyclerview android

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.