ListView shows empty message briefly before data is loaded

江枫思渺然 提交于 2019-12-22 04:07:34

问题


I've created a ListFragment using the following custom layout which adds a TextView to show a message when the list is empty:

<?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/android:list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>

  <TextView android:id="@id/android:empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/vsl_empty"
            android:gravity="center"
            android:layout_gravity="center"
            android:textColor="#8C8C8C" />
</LinearLayout>

The data is loaded using a custom adapter that derives from CursorAdapter. When there is data in the database, the list very briefly shows the empty list message until the data is loaded, at which point the list gets populated.

How can I prevent the empty list message from briefly appearing because the list hasn't been populated yet?

Update

I want to emphasise that I'm using a ListFragment with a layout that contains a ListView with the special id android:list, and a TextView with the special id android:empty, as explained here.

Setting android:visibility="invisible" doesn't affect the visibility. I suspect this is because it gets set to visible internally.


回答1:


The provided answers were on the right track. I indeed had to set the TextView to invisible, but the correct location is in onCreateLoader:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
  mListView.getEmptyView().setVisibility(ListView.GONE);

  return new CursorLoader(getActivity(), CONTENT_URI, null, null, null, null);
}

Also, it's not necessary to make the empty view visible again: this is done automatically by the framework.




回答2:


How about adding the following to the TextView

android:visibility="invisible"

to the XML layout file, and then making the TextView visible if required.




回答3:


You will have to set the TextView visibility to invisible, and only once you have loaded the data --that is onLoadComplete-- you will have to set it as the empty view for the ListView by doing setEmptyView(textView);

If you do it in XML or you do it before your results are loaded, the view will be shown since the list is effectively empty. So you have to wait until the results are loaded to set the empty view.



来源:https://stackoverflow.com/questions/14124326/listview-shows-empty-message-briefly-before-data-is-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!