主要是為了顯示W(wǎng)ebview高度折疊,如果大于600,則顯示600,點(diǎn)擊展開全部,再展開!如果小于600, 則按照實(shí)際高度顯示,不顯示折疊效果!
效果如下: - 點(diǎn)擊紫色框,做展開這顯示。



難點(diǎn)1:獲取Webview顯示內(nèi)容高度
2. 折疊后,點(diǎn)擊展開,不刷新內(nèi)容不顯示的問題
3. 當(dāng)前頁面如果未登錄,點(diǎn)擊登錄后,回到頁面內(nèi)容不顯示的bug
以上是實(shí)際實(shí)現(xiàn)過程的一點(diǎn)問題記錄。。。
So, do it as sequence..
1. 由于Webview的onPageFinished是Webview加載結(jié)束,但是并不一定表示完全就渲染到了界面,所以如果此時你立即用獲取控件高度的方法 MonkeyLei:Android中獲取控件寬高的方法集合 , 所以我采取了延遲500ms后獲取 - 注釋部分我嘗試了一堆方法,都不準(zhǔn), 這是一個其中嘗試過的國外帖子 https://stackoverflow.com/questions/23093513/android-webview-getcontentheight-always-returns-0
@Override
public void onPageFinished(WebView view, String url) {
if (asd_contentWvH <= 0 && (view.getId() == R.id.asd_contentWv)) {
// 試了這么多方法都不能正確獲取控件的高度
// view.loadUrl("javascript:( function () { var h = document.body.scrollHeight; window.HTMLOUT.toString(h); } ) ()");
// // 獲取渲染后的Webview高度,然后做高度限定,之后再展開
// ViewTreeObserver observer = asd_contentWv.getViewTreeObserver();
// observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// @Override
// public void onGlobalLayout() {
// asd_contentWv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// //int width = asd_contentWv.getMeasuredWidth();
// Log.e("test", "getMeasuredHeight=" + view.getMeasuredHeight());
// asd_contentWvH = (int) (view.getMeasuredHeight());
// //Log.e("test", "width=" + width);
// Log.e("test", "heightsssss=" + view.getHeight());
// Log.e("test", "heightsssss=" + asd_contentWv.getHeight());
// Log.e("test", "height=" + asd_contentWvH);
// Log.e("test", "height2=" + asd_contentWv.getContentHeight());
// Log.e("test", "getContentHeight=" + view.getContentHeight());
// Log.e("test", "getScaleY=" + view.getScaleY());
// Log.e("test", "getScaleX=" + view.getScaleX());
// Log.e("test", "height3=" + view.getContentHeight() * 1.5f);
// Log.e("test", "height3=" + view.getContentHeight() * view.getScaleY());
// ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) asd_contentWv.getLayoutParams();
// Log.e("test", "height4=" + layoutT.height);
// if (asd_contentWvH > 600) {
// ScreenUtil.setViewGroupHNoRatio(asd_contentWv, 600);
// asd_contentWv_showAll.setVisibility(View.VISIBLE);
// } else {
// ScreenUtil.setViewGroupHNoRatio(asd_contentWv, asd_contentWvH);
// }
// //Log.e("test", "height2=" + asd_contentWv.getContentHeight());
// }
// });
// 獲取Webview控件高度,只獲取一次
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//int contentHeight = view.getContentHeight();
int viewHeight = view.getHeight();
asd_contentWvH = viewHeight;
if (asd_contentWvH > 600) {
ScreenUtil.setViewGroupHNoRatio(asd_contentWv, 600);
asd_contentWv_showAll.setVisibility(View.VISIBLE);
} else {
ScreenUtil.setViewGroupHNoRatio(asd_contentWv, asd_contentWvH);
}
}
}, 500); // 500ms作為登錄后刷新,時間太短!導(dǎo)致獲取的高度有問題
}
}
上面>600, 以及隱藏顯示是否展示全部內(nèi)容控件等邏輯。
**2. **然后控制顯示隱藏邏輯 - 展開后需要刷新顯示內(nèi)容。我直接采取了外層ScrollView的滾動的方法來間距實(shí)現(xiàn)。 期間也嘗試了一些其他方式,發(fā)現(xiàn)不太穩(wěn)定啊。。。而且還不能用NetscrollView,有知道的不,小鍋鍋。。。 看來,知識還得逐步深入!
/**
* 顯示所有支持力度內(nèi)容
*/
@OnClick(R.id.asd_contentWv_showAll)
public void contentWv_showAll() {
if (asd_contentWv_showAll.getText().toString().contains("全部")) {
asd_contentWv_showAll.setText("收起");
ScreenUtil.setViewGroupHNoRatio(asd_contentWv, asd_contentWvH);
// 外部需要scrollview嵌套才能刷新,發(fā)現(xiàn)還是有問題。。。
//asd_contentWv.reload();
// 這樣也可以實(shí)現(xiàn)刷新
asd_scllView.scrollBy(0, 1);
} else {
asd_contentWv_showAll.setText("查看全部內(nèi)容");
ScreenUtil.setViewGroupHNoRatio(asd_contentWv, 600);
}
}
3. 另外我登錄后刷新當(dāng)前頁面,發(fā)現(xiàn)asd_contentWv不顯示。。原來是一開始進(jìn)入頁面的時候,我設(shè)置了一個空的字符串給Webview,然后我獲取寬高那里就獲取到了一個26,顯然不對。。最后我再設(shè)置內(nèi)容上去,發(fā)現(xiàn)高度也不對勁。。臥槽。。。不知道咋了。。

4. 另外,網(wǎng)友說不要scrollview里面不要嵌套一些個Webview了。。。。再研究?...后面整理下工程,然后深入做個研究吧。。。。┭┮﹏┭┮
這幾個問題先這樣記錄。。另外我自己搞的so庫,沒有打包x86,導(dǎo)致模擬器不能安裝。。我后面重新打包了一個支持x86的就可以了。。。

先醬紫吧。。。