I'm guessing you have height set to 'wrap_content'. The behavior you'r observing is because of a bug in the WebView and has been fixed in 4.4 (KitKat).
There is no good workaround for the bug - you could try temporarily forcing the WebView's height back to 0 every time the accordion is clicked but that will cause a glitch. Alternatively - if you control your content you could change the JavaScript that runs when the accordion is clicked to tell the WebView that it should shrink back to the previous height:
class MyWebView {
private int previousHeight = -1;
@Override
public void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
previousHeight = h;
}
public void accordionClicked() {
// I'm assuming the accordion is a toggle, so if you click it once
// it expands, you click it again - it shrinks.
LayoutParams lp = getLayoutParams();
if (lp.height == LayoutParams.WRAP_CONTENT)
lp.height = previousHeight;
else
lp.height = LayoutParams.WRAP_CONTENT;
setLayoutParams(lp);
}
}
You would then need to use addJavaScriptInterface
to expose a way for your JavaScript to call accordionClicked
:
class JsInterface {
private final WebView webView;
public JsInterface(WebView webView) {
this.webView = webView;
}
@JavascriptInterface
public void onAccordionClicked() {
webView.post(new Runnable() {
@Override
public void run() {
webView.accordionClicked();
}
});
}
}
You'd then register this interface in the same place you new up the WebView:
webView.addJavaScriptInterface("jsInterface", new JsInterface(webView);
Finally, call it in your JavaScript:
function accordionClicke() {
...
jsInterface.onAccordionClicked();
}
If your accordion is more complicated you could calculate the height of your content in JavaScript and pass it back to the WebView:
jsInterface.onAccordionClicked(document.body.clientHeight);
And then use that to set the right height:
public void accordionClicked(int heightCss) {
LayoutParams lp = getLayoutParams();
lp.height = (int) (heightCss * getScale());
setLayoutParams(lp);