Learn how to use ViewStub instances and how to use TextView with Drawable, viewstubdrawable
In a project, we may have multiple data sources, such:
It contains ListView and empty information displayed when the retrieved data is empty. The displayed empty messages vary depending on the project or task you click. A. There are no items to add to favorites, and B does not currently add to favorites.
In fact, there are many implementation methods. Can also be implemented. However, viewStub is the most appropriate here.
Let's first look at the layout of this Activity. Layout file:
<
FrameLayout android: id = "@ + id/layoutFrm"
android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background
= "@ Color/background_color"> <ListView xmlns: ptr = "http://schemas.android.com/apk/res-auto"
Android: id = "@ + id/lstV_fav_task" ----------- you know ------- ptr: ptrMode
= "PullFromEnd"> </com. handmark. pulltorefresh. library. PullToRefreshListView> <ViewStub android: id = "@ + id/viewStub"
android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout
= "@ Layout/activity_task_favourite_nodata"/> </FrameLayout>
Initialize in the code! Where is the activity_task_favorite_nodata of the layout file loaded by ViewStub?
View layout:
<? Xml version = "1.0" encoding = "UTF-8"?> <TextView xmlns: android = "http://schemas.android.com/apk/res/android"
android:id="@+id/txtV_nodata" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawablePadding="20dp" android:drawableTop="@drawable/favourite_nodata" android:gravity="center_horizontal" android:text="@string/my_task_fav_nodata" android:textColor
= "@ Color/listview_header_textcolor"/>
It means that a layout is actually a Textview. After a long time, I thought I had to create a Textview and a tall Imageview. It was so weak that I couldn't understand what I wrote.
(This is the drawable/favourite_nodata.png file)
What should I do in the code? Set it to invisible (pre-loaded) first. Not difficult. The key brother does not learn.
LayoutEmpty =
(TextView) viewStub.inflate(); layoutEmpty.setVisibility(View.GONE);
You must obtain the data .. There must be a method. Here:
private void getFavProjects()
In the obtained data, Judge datalist. size... Yes... Here you know
private void setEmptyViewByDataList(int type) { if (datalist.size() > 0) { layoutEmpty.setVisibility(View.GONE); } else { if (type == TYPE_PROJECT) layoutEmpty.setText(R.string.my_project_fav_nodata); else layoutEmpty.setText(R.string.my_task_fav_nodata); layoutEmpty.setVisibility(View.VISIBLE); } }