。
我有一个listView显示一些项目,我还使用了一个空视图,以防我的适配器没有listView的项目可以显示。屏幕上的空视图一秒钟,然后加载项目并在ListView中显示它们。
我的活动onCreate
看起来像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_exams);
list = (ListView) findViewById(R.id.favoriteExamsList);
View empty = findViewById(R.id.favoriteExamsListEmptyView);
list.setEmptyView(empty);
new readingFavFileTask().execute(UtilityFuctions.FAV_EXAMS_FILE_NAME);
}
我的布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/favoriteExamsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="@+id/favoriteExamsListEmptyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/favoriteExamsEmptyViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="You do not have any favorite Exams.Please add Exams using below button."
android:textSize="20dp" />
<Button
android:id="@+id/favoriteExamsAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickAddExams"
android:text="Add Exams"
>
</Button>
</LinearLayout>
</LinearLayout>
如果我们在适配器中有数据,我的确确保空白的视图也永远不会出现。/p>
我找到了答案。我只需要在设置适配器列表并完美工作后的Asynctask onpostexecute()方法中应用空视图list.setEmptyView(empty)
。