How can I implement scroll-to-hide address bar on WebView?
I want to implement an address bar like 开发者_如何转开发the default Browser app which can be hidden if a user scrolls a webpage downward and shown again when the user scrolls the webpage to the top then scroll upward.
Thanks!
set an onTouchListener for you webview that checks wv.getScrollY() to see if it is 0. Like this:
mWebView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me){
if(me.getAction() == MotionEvent.ACTION_UP){ //You might try with MotionEvent.ACTION_MOVE also, but you'll get way more calls.
if(mWebView.getScrollY() == 0){
//You are at the top, do what you need to do in order to show your address bar.
}
}
return false;
}
});
This example will work if the user scrolls with their finger and releases their finger all the way at the top. Which isn't going to handle all of the possible ways that the user scrolled to the top. I think to handle them all the ideal solution is to Override WebView and add a Listener to it that will send you a call whenever getScrollY() changes from anything to zero.
try {
Method m = WebView.class.getMethod("setEmbeddedTitleBar", new Class[] { View.class });
m.invoke(mWebView, url_bar);
}
catch(Exception e) {
}
when mWebView
is the web view (..) and the url_bar
is the view you want at the top.
edit
read more here and pay attention to the comments in the accepted answer.
精彩评论