前提是,如何获取GridView 中总共有多少行呢?

解决方案 »

  1.   

    后来看书,自己也到了这个count属性,嘿嘿……
      

  2.   

    @Override
    protected void onAttachedToWindow() {
      super.onAttachedToWindow();
      measureHeight();
    }private void measureHeight(){
    int childHeight = 0;
    mItemCount = getAdapter() == null ? 0 : getAdapter().getCount();
    final int count = mItemCount;
    if (count > 0) {
    final View child = getAdapter().getView(0, null, null);
    AbsListView.LayoutParams p = (AbsListView.LayoutParams)child.getLayoutParams();
    if (p == null) {
    p = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    child.setLayoutParams(p);
    }

    int childHeightSpec = getChildMeasureSpec(
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);
    int childWidthSpec = getChildMeasureSpec(
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.width);
    child.measure(childWidthSpec, childHeightSpec);
    childHeight = child.getMeasuredHeight();
    }
    int ourSize = 0;
    for (int i = 0; i < count; i += mNumColumns) {
                ourSize += childHeight;
            }
            int heightSize = ourSize;
            android.view.ViewGroup.LayoutParams params = getLayoutParams();
            params.height = heightSize;
            setLayoutParams(params);
    }