0

我有一个具有以下属性的 GridView:

<GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/PhoneImageGrid"
        style="@style/PhotoGridLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:alwaysDrawnWithCache="true"
        android:clipChildren="true"
        android:columnWidth="100dp"
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit"
        android:padding="4dp"
        android:scrollbars="none"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:stretchMode="columnWidth"
        android:verticalSpacing="4dp" />

这是网格项目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

现在,当我运行应用程序时,我得到了一个 gridview,纵向看起来一切正常,当我将其变为横向时,col 宽度正在更改,但我希望项目的内容也根据新的 col 宽度按比例缩放(item_height = item_width = 新列宽度)。我怎样才能做到这一点?谢谢

4

1 回答 1

0

设置获取gridview col宽度有点棘手。一个你得到了 col 宽度,你可以将 imageview 高度设置为 col 宽度并通知适配器更改。(取自 android 示例的代码:https ://developer.android.com/training/displaying-bitmaps/index.html )

以下是测量 col 宽度的方法:

imagegrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(imagegrid.getWidth()
                            / (mImageThumbSize + mImageThumbSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (imagegrid.getWidth() / numColumns) - mImageThumbSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });

在适配器中创建这些方法:

public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            // mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

将初始图像大小设置为:

mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT);

这是完整的适配器类:

public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            // mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

        public int getCount() {
            return images.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageItem item = images.get(position);
            holder.imageview.setId(position);

            // holder.imageview.setImageBitmap(item.img);
            holder.imageview.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (holder.imageview.getLayoutParams().height != mItemHeight) {
                holder.imageview.setLayoutParams(mImageViewLayoutParams);
            }
            holder.imageview.setImageBitmap(item.img);
            holder.checkbox.setChecked(item.selection);
            return convertView;
        }
    }
于 2013-02-03T17:06:01.183 回答