Android自定义视图保存和恢复状态

时间:2016-03-17 22:33:40

标签: android android-view android-custom-view

我有自定义组件扩展ImageView。方法onSaveInstanceState和onRestoreInstanceState正常工作,但不幸的是当我点击视图mIsSelectedState = false

我该如何解决这个问题?

public class CustomImageView extends ImageView {

private boolean mIsSelectedState;
...

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    setSaveEnabled(true);

    TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr, 0);
    ...
    attributes.recycle();

    mPaintImage = new Paint(Paint.ANTI_ALIAS_FLAG);
    ...
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (!isClickable()) {
        mIsSelectedState = false;
        return super.onTouchEvent(event);
    }

    ...

    invalidate();
    return super.dispatchTouchEvent(event);
}

...

@Override
protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState savedState = new SavedState(superState);

    savedState.mSelected = mIsSelectedState;
    return savedState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if(!(state instanceof SavedState)) {
        super.onRestoreInstanceState(state);
        return;
    }

    SavedState savedState = (SavedState)state;
    super.onRestoreInstanceState(savedState.getSuperState());
    setSelectedState(savedState.mSelected);
    setBorderColor(mBorderSelectedColor);
}

private void setSelectedState(boolean isSelected) {
    mIsSelectedState = isSelected;
}

...

private static class SavedState extends BaseSavedState {
    boolean mSelected;

    SavedState(Parcelable superState) {
        super(superState);
    }

    private SavedState(Parcel in) {
        super(in);
        mSelected = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeByte((byte) (mSelected ? 1 : 0));
    }

    public static final Parcelable.Creator<SavedState> CREATOR =
            new Parcelable.Creator<SavedState>() {
                public SavedState createFromParcel(Parcel in) {
                    return new SavedState(in);
                }
                public SavedState[] newArray(int size) {
                    return new SavedState[size];
                }
            };
}

1 个答案:

答案 0 :(得分:-3)

saveStates 的正确方法如下:

首先覆盖onSaveInstanceState()

 @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.puBoolean("myconstantKey", mIsSelectedState);
    }

然后,只要它恢复,你应该重写:

 @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mIsSelectedState = savedInstanceState.getInt("myconstantKey");
}

如果您在活动

之间切换,也可以在onCreate上执行此操作
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
           mIsSelectedState = savedInstanceState.getBoolean("myconstantKey");
        }

        ...
    }

记住Android活动生命周期很棘手,但一旦掌握了它,一切都会更好:)

希望它有所帮助。