Reputation: 529
I have a vertical LinearLayout and a Listview and a big Button inside. All is inside of a Fragment.
What I want to do is to show empty list when nothing is found after performing some action.
Since it's Fragment not a ListActivity I can't use the concept of empty view inside of the ListView to be shown when the list is empty.
I'm using this code to show empty list (called in the (MyFragment extends Fragment)->onCreateView method):
ListView list2=(ListView)rootView.findViewById(R.id.scans_list);
View emptyView = inflater.inflate(R.layout.scanslist_empty,null);
((ViewGroup)list2.getParent()).addView(emptyView, 0);
list2.setEmptyView(emptyView);
This works, but while initially the list is above the button, the empty list is below the button. Can it be fixed or do I have to change the whole approach?
Upvotes: 0
Views: 2533
Reputation: 14710
Add the emptyView (TextView
, ImageView
, any View
implementation you wish) in your fragment layout, make it visible="gone"
and use that instead of what you currently have. So the code should change to this:
ListView list2=(ListView)rootView.findViewById(R.id.scans_list);
View emptyView = rootView.findViewById(R.id.my_empty_view);
list2.setEmptyView(emptyView);
In this way, when the adapter checks if it's empty, the listView will automatically change its visibility to GONE and the empty view, if it exists (in your case, true), will be set to VISIBLE.
Upvotes: 11