Skip to content Skip to sidebar Skip to footer

How Does Grid View Change Column Number Runtime?

I am trying to change gridView column number. I call setNumColumn() and invalidateViews() to update the view. However, the grid's cell width will not change dynamically. I set the

Solution 1:

Problem Solved:

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

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();
    }

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

Thanks


Solution 2:

May be you could get help through this nice sample offered by Google http://developer.android.com/shareables/training/BitmapFun.zip The following code which locate in the ImageGridFragment.java is used to handle column number change dynamically during runtime such as screen orientation change when rotate the device:

Here is the comments:This listener is used to get the final width of the GridView and then calculate the number of columns and the width of each column. The width of each column is variable as the GridView has stretchMode=columnWidth. The column width is used to set the height of each view so we get nic square thumbnails.

mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (mAdapter.getNumColumns() == 0) {
                        final int numColumns = (int) Math.floor(
                                mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                        if (numColumns > 0) {
                            final int columnWidth =
                                    (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                            mAdapter.setNumColumns(numColumns);
                            mAdapter.setItemHeight(columnWidth);
                            if (BuildConfig.DEBUG) {
                                Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                            }
                        }
                    }
                }
            });

    return v;

Solution 3:

in xml u have to provide this attribute..

 android:stretchMode="columnWidth" . In your adapter depending on the size(by  `getCount()`) of the list colomn will create dynamically..

Post a Comment for "How Does Grid View Change Column Number Runtime?"