如何网格视图更改列数运行?网格、视图

2023-09-05 05:05:14 作者:祭夜

我试图改变GridView的列数。我称之为 setNumColumn() invalidateViews()更新视图。

I am trying to change gridView column number. I call setNumColumn() and invalidateViews() to update the view.

然而,在网格的单元格的宽度不会动态变化。我设置了 stretchMode =columnWidth中,但没有奏效。

However, the grid's cell width will not change dynamically. I set the stretchMode="columnWidth", but it didn't work.

推荐答案

问题解决了:

在网格视图中的子视图不重新计算其布局时,网格视图更改列上运行。

The child view in the grid view do not re-calculate its layout when the grid view changes the column on runtime.

在你BaseAdapter,你应该叫forceLayout()来计算其布局。

In your BaseAdapter, you should call forceLayout() to calculate its layout.

@Override
public View getView(final int position, View convertView,
        ViewGroup parent) {


    if (null == convertView) {
        convertView = mInflater.inflate(
                R.layout.mnm_customer_user_thumbnail, null);

        control.txtUserName = (TextView) convertView
                .findViewById(R.id.mnmTxtStudentName);
        control.image = (ImageView) convertView
                .findViewById(R.id.mnmImageStudent);
        control.imageCheckBox = (ImageView) convertView
                .findViewById(R.id.mnmImageCheckBox);
        control.mnmOuterBounder = (RelativeLayout) convertView
                .findViewById(R.id.mnmOuterBound);
        convertView.setTag(control);
    } else {
        convertView.forceLayout();
    }

...
...
...
...

感谢