开发者

Problem handling orientation change in custom view

I wrote a custom view that extends LinearLayout. It holds two views, a "header" and a "body". The custom view maintains "expanded" state that toggles on and off whenever the header view is clicked. When expanded, the body is VISIBLE and when not expanded, the body is GONE. I want to preserve the expanded state of this view when the device orientation changes, so I wrote onSaveInstanceState and onRestoreInstanceState to save an expanded flag.

One problem is that if I reorient the device when any of these views is expanded, then the entire activity is redrawn without changing the orientation of the views. (If I change from portait to landscape, the screen stays in portrait.) If none of the custom views are expanded, the activity's view tree is reoriented correctly.

Here's my logic for the view's onRestoreInstanceState:

protected void onRestoreInstanceState(Parcelable state) {
    SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.getSuperState());
    if (ss.expanded != mHeader.isExpanded()) {
   开发者_JAVA技巧     mHeader.toggleExpanded();
    }
    mBody.setVisibility(ss.expanded ? VISIBLE : GONE);
}

What am I doing wrong?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜