ant
ant

Reputation: 407

gridlayout's children visibility gone

This is my .xml.
When I touch the cell - visibility sets as View.GONE, but it just disappearing as View.INVISIBLE. There is an empty space in the place when cell was. The size of cells is fixed.

How to configurate the GridLayout to work properly?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridLayout
    android:id="@+id/grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#DDDDDD"
    android:columnCount="3"
    android:rowCount="3" >

    <LinearLayout
        android:id="@+id/lin1"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#0099cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin2"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#99CC00"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin3"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#FFBB33"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin4"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#ff4444"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin5"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#33b5e5"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin6"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#aa66cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin7"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#9933cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin8"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#669900"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin8"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#ff8800"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>
</GridLayout>

</LinearLayout>

this is code of hideCell method:

public void hideCell(View v) {
    v.setVisibility(View.GONE);
}

Upvotes: 4

Views: 2525

Answers (2)

Example:

package ua.vsgroup.widgets;

import android.content.Context;
import android.support.v7.widget.GridLayout;
import android.util.AttributeSet;
import android.view.View;

public class vsGridLayout extends GridLayout {

    View[] mChild = null;

    public vsGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public vsGridLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public vsGridLayout(Context context) {
        this(context, null);
    }

    private void arrangeElements() {

        mChild = new View[getChildCount()];
        for (int i = 0; i < getChildCount(); i++) {
            mChild[i] = getChildAt(i);
        }

        removeAllViews();
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() != GONE)
                addView(mChild[i]);
        }
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() == GONE)
                addView(mChild[i]);
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        arrangeElements();
        super.onLayout(changed, left, top, right, bottom);

    }


}

Upvotes: -1

ant
ant

Reputation: 407

Ok, according to Android documentation of GridLayout:

Interpretation of GONE

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. These statements apply equally to rows as well as columns, and to groups of rows or columns.

so i solve this problem by creating my own GridView class.

Upvotes: 2

Related Questions