這篇文章主要介紹了Android如何實(shí)現(xiàn)類似UC瀏覽器的效果向上滑動(dòng)地址欄隱藏功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到濱江網(wǎng)站設(shè)計(jì)與濱江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋濱江地區(qū)。思路
要求
ScrollView 嵌套 地址欄 和 WebView
手指滑屏向下滾動(dòng)(網(wǎng)頁(yè)向上),如果網(wǎng)頁(yè)有滾動(dòng)條,首先把 地址欄 滾動(dòng)到消失,然后 WebView 才開(kāi)始滾動(dòng);
手指滑屏向上滾動(dòng)(網(wǎng)頁(yè)向下),如果地址欄隱藏,那么 地址欄 首先慢慢顯示,然后 WebView 才開(kāi)始滾動(dòng)。
實(shí)現(xiàn)方案
根據(jù) View 的 onInterceptTouchEvent 和 onTouchEvent 原理。把 ScrollView 設(shè)置為 WebView 的一個(gè)變量,在 WebView的 onInterceptTouchEvent 方法里檢測(cè)到 MotionEvent.ACTION_DOWN 事件后中斷事件,在 WebView 的 onTouchEvent 事件中根據(jù)具體情況決定是把 MotionEvent.ACTION_MOVE 事件傳送給 ScrollView 還是留給自己
由于MotionEvent.ACTION_MOVE 事件傳送給 ScrollView 后無(wú)法在一次 Touch 事件中再接收,所以會(huì)導(dǎo)致如果有地址欄,向下滑動(dòng)第一次只能滑動(dòng)到 ScrollView 消失
+
Hack網(wǎng)頁(yè),加入JS腳本,前行讓網(wǎng)頁(yè)頂部空出來(lái)一段空白,空白處覆蓋地址欄
優(yōu)點(diǎn)是WebView大小不變化,容易控制
缺點(diǎn)是比較復(fù)雜要處理各種網(wǎng)頁(yè)元素,各種 position 情況,實(shí)現(xiàn)復(fù)雜,效率低
由手勢(shì)接管所有觸發(fā)操作,再由它分發(fā)給需要滾動(dòng)的控件
本文方法
資源
SrollView下面包含節(jié)點(diǎn)地址欄,WebView控件
ScrollView繼承自 ScrollView
onTouchEvent 中阻止 MotionEvent.ACTION_MOVE 事件 public class MyScrollView extends ScrollView { public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public boolean onTouchEvent(MotionEvent ev) { if(ev.getAction() == MotionEvent.ACTION_MOVE) { return true; } return super.onTouchEvent(ev); } }
MyWebView繼承自 WebView
onTouchEvent 中阻止 MotionEvent.ACTION_MOVE 事件
onDrawListner
計(jì)算豎直滾動(dòng)范圍
public class MyWebView extends WebView { public interface MyWebViewListener { void afterDraw(WebView webView); } private MyWebViewListener mListener; private int mMoveCheckedCnt; public MyWebView(Context context) { super(context); } public MyWebView(Context context, AttributeSet attrs) { super(context, attrs); } public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) { super(context, attrs, defStyleAttr, privateBrowsing); } public void setListener(MyWebViewListener listener) { mListener = listener; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mMoveCheckedCnt = 0; flingScroll(0, 0); break; case MotionEvent.ACTION_MOVE: mMoveCheckedCnt++; return false; case MotionEvent.ACTION_UP: if(mMoveCheckedCnt >= 2) { event.setAction(MotionEvent.ACTION_CANCEL); mMoveCheckedCnt = 0; } break; } return super.onTouchEvent(event); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); MyWebViewListener listener = mListener; if(listener != null) { listener.afterDraw(this); } } public int getVScrollRange() { int v = computeVerticalScrollRange() - computeVerticalScrollExtent(); if(v < 0) { v = 0; } return v; } }
主窗口
GlobalLayoutListener 獲取地址欄和滾動(dòng)視圖高度
GestureDetector 邏輯分發(fā) - 決定是滑動(dòng)webview還是改變webview高度從而改變ScrollView滾動(dòng)范圍(ScrollView總是滾動(dòng)到最底)
WebView 重畫(huà)之后檢測(cè)當(dāng)前地址欄偏移
public class MainActivity extends AppCompatActivity implements MyWebView.MyWebViewListener { MyWebView mWebView; GestureDetector mGesture = null; View mToolBar; int mToolBarHeight; MyScrollView mScrollView; int mScrollViewHeight; int mScrollOffset; EditText mUrlEdit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (MyWebView) findViewById(R.id.webView); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }); mWebView.setListener(this); mWebView.loadUrl("http://www.sohu.com"); mUrlEdit = (EditText) findViewById(R.id.urlEdit); findViewById(R.id.goButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = mUrlEdit.getText().toString(); if (!url.startsWith("http://") && !url.startsWith("https://")) { url = "http://" + url; } mWebView.loadUrl(url); } }); mToolBar = findViewById(R.id.toolBar); mScrollView = (MyScrollView)findViewById(R.id.scrollView); ScrollView scrollView = (ScrollView) mScrollView; findViewById(R.id.root).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mToolBarHeight = mToolBar.getHeight(); mScrollViewHeight = mScrollView.getHeight(); adjustScrollView(); } }); mGesture = new GestureDetector(this, new GestureListener()); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { mGesture.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } @Override public void afterDraw(WebView webView) { if (mWebView.getVScrollRange() < mScrollOffset) { mScrollOffset = mWebView.getVScrollRange(); adjustScrollView(); } } class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDoubleTap(MotionEvent e) { Log.e("Temp", "onDoubleTap"); return super.onDoubleTap(e); } @Override public boolean onDown(MotionEvent e) { Log.e("Temp", "onDown"); return super.onDown(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.e("Temp", "onFling:velocityX = " + velocityX + " velocityY" + velocityY); int effectX = (int) velocityX; int effectY = (int) velocityY; if (effectOnScrollViewByScroll((velocityY < 0 ? 1 : -1) * 8000)) { effectY = 0; } mWebView.flingScroll(-effectX, -effectY); return super.onFling(e1, e2, velocityX, velocityY); } @Override public void onLongPress(MotionEvent e) { Log.e("Temp", "onLongPress"); super.onLongPress(e); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.e("Temp", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY); int effectX = (int) distanceX; int effectY = (int) distanceY; if (effectOnScrollViewByScroll((int) distanceY)) { effectY = 0; } mWebView.scrollBy(effectX, effectY); return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onSingleTapUp(MotionEvent e) { Log.e("Temp", "onSingleTapUp"); return super.onSingleTapUp(e); } } private boolean effectOnScrollViewByScroll(int distanceY) { if (distanceY > 0) { // scroll up, the web will scroll down if (mScrollOffset >= mToolBarHeight || mScrollOffset >= mWebView.getVScrollRange()) { return false; } mScrollOffset += distanceY; if (mScrollOffset > mToolBarHeight) { mScrollOffset = mToolBarHeight; } if (mScrollOffset > mWebView.getVScrollRange()) { mScrollOffset = mWebView.getVScrollRange(); } } else { if (mScrollOffset <= 0) { return false; } mScrollOffset += distanceY; if (mScrollOffset <= 0) { mScrollOffset = 0; } } adjustScrollView(); return true; } private void adjustScrollView() { Log.e("Temp", "offset is " + mScrollOffset); ViewGroup.LayoutParams layoutParams = mWebView.getLayoutParams(); int newHeight = (mScrollViewHeight - mToolBarHeight) + mScrollOffset; Log.e("Temp", "newHeight is " + newHeight + ", layoutParams.height" + layoutParams.height); if (newHeight != layoutParams.height) { layoutParams.height = newHeight; mWebView.setLayoutParams(layoutParams); new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { mScrollView.fullScroll(ScrollView.FOCUS_DOWN); } }); } } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)類似UC瀏覽器的效果向上滑動(dòng)地址欄隱藏功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!