Android_AdapterView_setEmptyView, setemptyview

Source: Internet
Author: User

Android_AdapterView_setEmptyView, setemptyview

When we use ListView or GridView, when the list is empty, we often need a Loading, a text prompt, or a special View to prompt users for operations, the setEmptyView () method is used at this time.

SetEmptyView () is actually the method of AdapterView, while ListView, GridView, ExpandableListView, which are commonly used in development, all inherit from AdapterView, so you can call this method directly, next let's take a look at how to use it:

The following uses TextView, which displays the text of a prompt by default, as an example:

1. Empty View and ListView are in the same layout File
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"    android:layout_height="match_parent">    <ListView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/list_view" />    <TextView         android:id="@+id/tv_empty"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="Loading data..." /></FrameLayout>
ListView listView = (ListView)findViewById(R.id.list_view);listView.setEmptyView(findViewById(R.id.tv_empty));
2. Empty View is stored in a separate layout file, which is generally applicable to complicated views or is intended to be reused in multiple places.

The setEmptyView () method is restricted. This View must be on the current View hierarchy node. Therefore, when we write it in a separate layout file, you need to add the View to the current View hierarchy node. Therefore, the following usage is required:

View emptyView = View.inflate(R.layout.empty_view, null);((ViewGroup)list.getParent()).addView(emptyView);ListView listView = (ListView)findViewById(R.id.list_view);listView.setEmptyView(emptyView);

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.