【问题标题】:Why does empty view in layout file shows up on screen even though listview has items?为什么即使列表视图有项目,布局文件中的空视图也会显示在屏幕上?
【发布时间】:2012-10-19 12:17:35
【问题描述】:

我有一个 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>

如果我们的适配器中有数据,我如何确保空视图永远不会显示一秒钟。我应该在检查适配器后将空视图附加到列表中,还是通过其他任何方式来做到这一点?

【问题讨论】:

  • 可能会显示空视图,因为任务没有立即完成设置ListView 的适配器。除非您确切知道执行任务需要多少时间,否则您无能为力。
  • 如果只是在我的 AsyncTask onPostExecute() 方法中执行 list.setEmptyView(empty),那不是有帮助吗?
  • 是的,先在任务的onPostExecute设置适配器后设置空视图。这样,第一次执行任务时不会出现空视图。当然,这意味着如果第一次运行任务需要 10 秒才能完成,在此期间用户将不会在屏幕上看到空视图。
  • 好的,让我试试……如果可行并且没有任何其他缺陷,请发布答案。

标签: android android-layout android-activity


【解决方案1】:

我找到了答案。在将我的适配器设置为列表后,我只需要在我的 AsyncTask onPostExecute() 方法中应用 Empty View list.setEmptyView(empty),它就可以完美地工作了。

【讨论】:

  • 我在 setListAdapter 使用 getListView().setEmptyView(theEmptyView) 之后将它设置在我的 ListFragment 的 onActivityCreated 中。电话setEmptyView 绝对是我需要的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多