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);