我改了一点,嵌套布局太多,如果是单一webview就比较好解决
mBinding.rvContentList.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mBinding.rvContentList.loadUrl("javascript:App.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
});
mBinding.rvContentList.addJavascriptInterface(this, "App");
@JavascriptInterface
public void resize(final float height) {
Log.e("TAG","promotion"+height);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setWebViewHeight(DisplayUtils.dipToPixels(getActivity(), (int)height));
//Toast.makeText(getActivity(), height + "", Toast.LENGTH_LONG).show();
//此处的 layoutParmas 需要根据父控件类型进行区分,这里为了简单就不这么做了
// mBinding.rvContentList.setLayoutParams(new ConstraintLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
}
});
}
private int mWebViewHeight;
public void setWebViewHeight(final int height) {
if (height !=mWebViewHeight && height >0) {
mBinding.rvContentList.getLayoutParams().height = height;
mBinding.rvContentList.requestLayout();
mWebViewHeight = height;
}
}
网友评论