如何在gridview中获取columncount

时间:2014-06-27 07:05:58

标签: android gridview

gridview noofColumn =“autofit”模式时,如何在Gridview中获取列数

如何获取列的单行号

<GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"

                android:background="@android:color/white"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth" />

2 个答案:

答案 0 :(得分:7)

您可以在java代码中获取列数。

GridView mGridView = (GridView)findViewById(R.id.gridview);
int column_coutnt = mGridView.getNumColumns();

第二种方式:

int columns = 0;
int children = mGridView.getChildCount();
if (children > 0) {
     int width = mGridView.getChildAt(0).getMeasuredWidth();
     if (width > 0) {
        columns = mGridView.getWidth() / width;
     }
 }            

答案 1 :(得分:1)

试试这样..

Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );