NestedScrollWebview實現與優化

2021-09-11 04:32:32 字數 2566 閱讀 1540

nestedscrollwebview實現與優化

原文如下: 

好久沒寫了,好像也沒什麼人關注我,呵呵,但我還是堅持寫一下,希望能幫到有需要的人! 

今天我來說一下nestedscrollwebview。

最近在弄乙個需求,我需要用到coordinatorlayout + webview 實現滾動互動效果,但要實現該效果子view必須是要實現nestedscrollingchild介面的,很可惜原生webview並沒有實現這介面。 

網上找到能實現這種需求的有兩種方法: 

1)nestedscrollview 巢狀 webview 

這種方法我試過,確實可以達到效果,但有乙個比較坑的問題。就是webview的高度變成網頁高度,導致網頁滾動事件的缺失,如果網頁需要監聽滾動事件實現某些效果時(如懶載入)這時就會失效。

2)nestedscrollwebview 

google官方並沒有提供這個控制項,網上能找到的就是rhlff大神寫的乙個控制項(我也用了這個控制項,但實際效果比較差,滾動時網頁會抖動。

為了實現需求,只能改大神的**了,看了一**神的**後我修改了事件分發時的邏輯,實測效果跟上述第一種方法效果相似且沒有***,詳細**如下:

public class nestedscrollwebview extends webview implements nestedscrollingchild   

public nestedscrollwebview(context context, attributeset attrs)

public nestedscrollwebview(context context, attributeset attrs, int defstyleattr)

private void init()

@override

public boolean ontouchevent(motionevent event)

int y = (int) event.gety();

event.offsetlocation(0, mnestedyoffset);

switch (action)

int oldy = getscrolly();

mlastmotiony = y - mscrolloffset[1];

int newscrolly = math.max(0, oldy + deltay);

deltay -= newscrolly - oldy;

if (dispatchnestedscroll(0, newscrolly - deltay, 0, deltay, mscrolloffset))

if(mscrollconsumed[1]==0 && mscrolloffset[1]==0)

break;

case motionevent.action_pointer_down:

case motionevent.action_up:

case motionevent.action_cancel:

stopnestedscroll();

result = super.ontouchevent(event);

break;

} return result;

} // nestedscrollingchild

@override

public void setnestedscrollingenabled(boolean enabled)

@override

public boolean isnestedscrollingenabled()

@override

public boolean startnestedscroll(int axes)

@override

public void stopnestedscroll()

@override

public boolean hasnestedscrollingparent()

@override

public boolean dispatchnestedscroll(int dxconsumed, int dyconsumed, int dxunconsumed, int dyunconsumed, int offsetinwindow)

@override

public boolean dispatchnestedprescroll(int dx, int dy, int consumed, int offsetinwindow)

@override

public boolean dispatchnestedfling(float velocityx, float velocityy, boolean consumed)

@override

public boolean dispatchnestedprefling(float velocityx, float velocityy)

}

Inotify實現實時備份與調優

inotify是一種強大的 細粒度的 非同步的檔案系統監控機制,linux核心2.6.13起,加入了inotify支援,通過inotify可以監控檔案系統中新增 刪除 修改 移動等各種事件,利用這個核心介面,inotify就可以監控檔案系統下的變化。安裝inotify 1 檢視核心是否支援 unam...

uitableview cell實現效能優化

如果你想要如絲般順滑的效果,那麼 1 每次都看一下有沒有能重用的 cell,而不是永遠重新新建 這個是 uitableview 的常識 2 載入放到後台程序去進行,滾出可視範圍的載入程序要 cancel 掉 3 圓角 陰影之類的全部 bitmap 化,或者放到後台 draw 好了再拿來用 4 cel...

Mysql集群與調優

mysql集群與調優 實驗背景 1 安裝mysql cluster相關軟體包。2 依次配置管理 資料 sql節點。3 啟動並測試mysql cluster集群架構。實驗方案 使用6臺rhel 6.4虛擬機器,其中sqla和sqlb作為sql節點,ndba和ndbb作為資料節點,mgmd作為管理節點,...