0

我有可扩展列表视图的布局。并且,每个子项都有gridview。但gridview 有自己的滚动和expandablelistview 也有。所以我必须为所有gridview设置固定长度,但是gridviews的数量是动态的。

我从这里找到了单个 gridview 的解决方案。

private OnGlobalLayoutListener mOnGlobalLayoutGridListener = new OnGlobalLayoutListener() {

    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {

        Debug.e("", "onGlobalLayout is called");

        if (gridView != null && gridView.getChildCount() > 0) {

            gridView.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);

            // gridView.get

            View lastChild = gridView
                    .getChildAt(gridView.getChildCount() - 1);

            int rows = gridView.getAdapter().getCount() / numColumns;
            int extra = gridView.getAdapter().getCount() % numColumns;

            if (extra > 0) {
                rows++;
            }

            int height = (int) (lastChild.getMeasuredHeight() * rows);

            gridView.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, height));
        }

    }
};

OnGlobalLayoutListener不引用特定的 Gridview。所以我不能将此侦听器用于所有网格视图,因为所有网格视图都有不同数量的项目。

看这里

在此处输入图像描述

谁能帮我?提前致谢。

4

1 回答 1

0

您可以像这样使用自定义 GridView ,然后您不必为所有 gridview 设置固定长度:

公共类 GridlistView 扩展 GridView{

public GridlistView(Context context, AttributeSet attrs) {
    super(context, attrs);
}


public void  onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
    int expendSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expendSpec);
}

}

在您的 xml 中使用 Gridlistview;

于 2014-04-22T09:15:00.320 回答