在ViewGroups中为每行拆分GridView

时间:2018-01-05 07:07:01

标签: android gridview android-viewgroup

我有一个程序用自定义适配器创建一个dinamic GridView,我想使用for循环来绕过这些元素。

问题是例如在一个4 x 4网格中,GridView只有一个ViewGroup,因此它有16个元素,但我想让4个ViewGroup分别在每个组中循环。

我该怎么做?

我的GridView布局:

<GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context="com.example.dante.puzzle.Joc"
        android:id="@+id/gridView1"
        android:gravity="center"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

</GridView>

我的自定义适配器单元:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:orientation="vertical">

        <TextView
                android:id="@+id/grid_item_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:layout_marginTop="3px"
                android:textSize="20px" >
        </TextView>

    </LinearLayout>

</LinearLayout>

在简历中,我想像这样迭代:

gridView = findViewById(R.id.gridView1);
        final int size = gridView.getChildCount();
        for (int i = 0; i < size; i++) {
            ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i);
            int childSize = gridChild.getChildCount();
            for (int k = 0; k < childSize; k++) {
                if (gridChild.getChildAt(k) instanceof TextView) {
                    gridChild.getChildAt(k).setVisibility(View.GONE);
                }
            }
         }

但我不能,因为我的GridView只有1(16)个ViewGroup,而不是4(4)个ViewGroup。

0 个答案:

没有答案