Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Last active January 6, 2018 23:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyboardr/5259888 to your computer and use it in GitHub Desktop.
Save keyboardr/5259888 to your computer and use it in GitHub Desktop.
Demonstrate how to use the empty view to show progress indication and handle empty state. Note: if using a normal Fragment, you should call ListView.setEmptyView() to assign the empty view to the ListView.
public class ContentListFragment extends ListFragment implements LoaderCallbacks<List<Content>>{
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onLoadFinished(Loader<List<Content>> loader, List<Content> content){
setListAdapter(new ContentListAdapter(content);
((ViewSwitcher) getView().findViewById(R.id.empty)).setDisplayedChild(1);
}
@Override
public Loader<List<Content>> onCreateLoader(int arg0, Bundle arg1) {
return new ContentLoader(getActivity());
}
@Override
public void onLoaderReset(Loader<List<Episode>> arg0) {
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<ViewSwitcher
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/content_empty" />
</ViewSwitcher>
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment