I have a listview with some items that can be marked as "done". There is also a togglebutton that says "hide done items".
However, when I do hide the items by setting setVisibility(View.GONE) there is still space left in the list..
Should not be that hard to toggle list items in a listview?
After checking many solutions none of which was solved my issue with the empty space so I decided to came up with my solution.
I had two main issues: 1) I had an empty space because of the view that I set its visibility to gone 2) I had also dividerHeight of 12dp, even if I had the first issue solved I still had the fixed divider height of the listview
Solution:
1.1) I added a boolean to the data of the list, to notify the adapter which of the items are skipped
1.2) I created an empty layout to simulate an "skipped item"
1.3) I have several types of views in my listview, selected item, regular item and now skipped item
only if the item is skipped the adapter inflate to an empty layout as shown on previous step, still I had the divider height issue
2) To fix divider height I changed the divider height to 0 instead of 12dp, each item that is not skipped I added another layout with transparent background (the divier color in my case should be transparent) and added bottom padding of 12dp
for example one of my items
maybe its not elegant but this solution worked for me
Are you trying to hide the whole list item? If so I guess that the list view won't like that because it is still calculating with the same amount of items. I don't think it will just ignore it because it's gone.
The clean solution would be to return another
getCount
and just ignore the items you want to hide. Or remove items from the internal used list. CallnotifyDataSetChanged
on the adapter when you modified the amount of items in the list.you should operate on the list adapter also...
I was able to solve this problem using Knickedi's solution and the comments under it. Just wanted to show my relatively complete adapter to clarify it a bit.
I have the class StockItem with fields to hold a range of data for a single stock item. For the custom ArrayAdapter, the constructor takes the complete list of StockItems retrieved from a database table, and any add/remove methods I may add in the future will also operate on this list (mList). However, I overrode getView(), and getCount() to read from a second list (mFilteredList) produced using the method filterList():
filterList(String search) is called from an OnQueryTextListener and removes list items whose descriptions don't match the entered text.
In the case of a large list, filterList() may be a problem on the main thread, but that is irrelevant for this question.
EDIT: The getItem(position) method must also be overridden to return the item from mFilteredList.
View.GONE is actually releasing space but the other elements might have been confined to their current positions. Try this. In the custom layout file (which acts as view for the list items),
Suppose if X is the UI element you want to be GONE, W is the element below X and Y is the element above X
In the custom Layout of your ListView, (Assuming it's a relative layout) Attach the top of W to the bottom of X. And then attach the top of the element X to the bottom of Y.
Changing the android:layout_height="wrap_content" to android:layout_height="fill_parent" fixed the issue.. was testing with a long list.. with a short list the same space was above the list.. Stupid mistake..
Thanks all for help.. everything is working now.