1

我有一个gridview,它根据gridview的大小和col宽度设置视图,当我们开始时一切都很好,但是当我改变方向时,视图被重新创建并且被选中的复选框被刷新,这是我不想要的。我需要保持滚动状态以及复选框状态。

这是我在getview中的代码:

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.checkbox.setId(position);
            holder.imageview.setId(position);
            holder.checkbox.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();
                    if (images.get(id).selection) {
                        cb.setChecked(false);
                        images.get(id).selection = false;
                    } else {
                        cb.setChecked(true);
                        images.get(id).selection = true;
                    }
                }
            });
            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = v.getId();
                    ImageItem item = images.get(id);
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    final String[] columns = { MediaStore.Images.Media.DATA };
                    Cursor imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            columns, MediaStore.Images.Media._ID + " = " + item.id, null, MediaStore.Images.Media._ID);
                    if (imagecursor != null && imagecursor.getCount() > 0) {
                        imagecursor.moveToPosition(0);
                        String path = imagecursor.getString(imagecursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                        File file = new File(path);
                        imagecursor.close();
                        intent.setDataAndType(Uri.fromFile(file), "image/*");
                        startActivityForResult(intent, VIEW_IMAGE);
                    }
                }
            });
            // 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);
            }
            bitmapFromId.DisplayImage(item.id, holder.imageview);
            // holder.imageview.setImageBitmap(item.img);
            holder.checkbox.setChecked(item.selection);
            return convertView;
        }

如果我设置 android:configChanges="orientation" 它不会更新我在网格中的视图

4

3 回答 3

0

您可以保留配置更改之前的状态并在更改后恢复它。您可以执行类似的操作,如下所示。

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle)
 */
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int scrollTo = savedInstanceState.getInt("firstItem");
    gridView.setSelection(scrollTo);
    setSelectedItem(savedInstanceState.getInt("selectedItem"));//A method to set the selected Item
}


/* (non-Javadoc)
 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    int scrollTo = gridView.getFirstVisiblePosition();
    outState.putInt("firstItem", scrollTo);
    int selectedItem = getSelectedImageItem();//A method to retrieve the selected item from the adapter
    outState.putInt("selectedItem", selectedItem);
    /*additionally you can pass the entire collection into the bundle and retrieve it as well 
    using android.app.Activity#onRetainNonConfigurationInstance*/
}

请通过以下链接了解更多信息,从长远来看,这将对您有所帮助。

处理运行时配置更改

于 2013-02-04T08:23:36.647 回答
0

在 androidmanifest.xml 中添加在您的活动中爆炸的项目

android:configChanges="orientation|screenSize|keyboardHidden"
于 2013-02-04T07:23:38.673 回答
0

将此添加到您的清单中

<activity
            android:name=".Your_Activity"
            android:configChanges="keyboardHidden|orientation" >

并在 Java 代码中进行更改以更新视图 Window.onOrientationChange

于 2013-02-04T07:55:05.937 回答