Reputation: 6187
I am hiding a child from a gridview by setting its visibility to View.GONE
. The problem is that even thought I set it to GONE, it becomes invisible but remais still a gap where it was located.
Is it possible to rearrange the gridview, so that the hidden elements don't occupy any space in the layout?
<GridView
android:id="@+activity_queues/gv_queue1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="200dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="none"
android:verticalSpacing="10dp" >
</GridView>
Upvotes: 6
Views: 2925
Reputation: 133
you can call this method after changing the visibility of views:
private void rearrangeVisibleViewsInGridLayout() {
int childCount=binding.gridLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = binding.gridLayout.getChildAt(i);
if(child==null) continue;
if(child.getVisibility()==View.GONE){
binding.gridLayout.removeViewAt(i);
}
}
}
Upvotes: 1
Reputation: 634
I'm struggling with this issue myself. Weirdly enough, the documentation presents this exact scenario but, after experimenting, it doesn't work:
https://developer.android.com/reference/android/widget/GridLayout.html
It states:
"For layout purposes, GridLayout treats views whose visibility status is GONE, as having zero width and height. This is subtly different from the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked view was alone in a column, that column would itself collapse to zero width if and only if no gravity was defined on the view. If gravity was defined, then the gone-marked view has no effect on the layout and the container should be laid out as if the view had never been added to it. GONE views are taken to have zero weight during excess space distribution. These statements apply equally to rows as well as columns, and to groups of rows or columns."
Did anyone find a better solution yet?
Upvotes: 0
Reputation: 12823
To remove a single item from your GridView, you have to remove it from the Adapter or the GridView's data source. Since, as you discovered, changing the visibility of a cell won't rearrange your layout, it will just show a blank cell for that item.
Upvotes: 2
Reputation: 5535
GONE should remove it completely and according to the documentation and my experience it no longer takes any space. So, if it is, then it is probably due to padding or margins from the elements above and below the GridView?
Upvotes: -1