Android gridview在更改方向时保持状态但需要更新视图

时间:2013-02-04 07:20:06

标签: android android-view android-gridview android-adapter android-adapterview

我有一个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”,它不会更新我在网格中的视图

3 个答案:

答案 0 :(得分:0)

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

android:configChanges="orientation|screenSize|keyboardHidden"

答案 1 :(得分:0)

在清单中添加此内容

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

并进行更改,以便在Window.onOrientationChange

的Java代码中更新视图

答案 2 :(得分: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*/
}

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

Handling Runtime config changes