时间:2021-05-21
思路
要求
ScrollView 嵌套 地址栏 和 WebView
手指滑屏向下滚动(网页向上),如果网页有滚动条,首先把 地址栏 滚动到消失,然后 WebView 才开始滚动;
手指滑屏向上滚动(网页向下),如果地址栏隐藏,那么 地址栏 首先慢慢显示,然后 WebView 才开始滚动。
实现方案
本文方法
资源
SrollView下面包含节点地址栏,WebView控件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/root" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <samples.zjc.com.testbrowserfeature.MyScrollView android:id="@+id/scrollView" android:scrollbars="none" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/toolBar" android:background="#5ff0" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/urlEdit" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/> <Button android:id="@+id/goButton" android:text="Go" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <samples.zjc.com.testbrowserfeature.MyWebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="100dp" /> </LinearLayout> </samples.zjc.com.testbrowserfeature.MyScrollView></RelativeLayout>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
计算竖直滚动范围
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 获取地址栏和滚动视图高度
GestureDetector 逻辑分发 - 决定是滑动webview还是改变webview高度从而改变ScrollView滚动范围(ScrollView总是滚动到最底)
WebView 重画之后检测当前地址栏偏移
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://"); 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); } }); } }}总结
以上所述是小编给大家介绍的Android 类似UC浏览器的效果:向上滑动地址栏隐藏功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
最近在开发浏览器碰到这么一个需求:点击地址栏的时候,需要全选并调出键盘,再次点击就取消全选显示光标。点击屏幕除地址栏其他位置时,键盘隐藏,隐藏光标。大部分浏览器
Chrome谷歌浏览器删除地址栏记录的方法比如,我输入错误一个地址为“mp。.uc.cn”,想单独删除它:1、在地址栏中输入&ldquo
向上滑动隐藏底部悬浮框,向下滑动显示悬浮框。使用pc端浏览器查看请把浏览器设置为手机浏览器模式。手机端触屏手指滑动方向varcurrntY=0;varlastY
以百度浏览器为例,手机地址栏是在:首先打开百度浏览器,然后点击右下角我的;再点击设置;在基本设置中,把地址栏输入提示按钮打开就可以了。 地址栏在IE地址栏中输
手机浏览器地址栏查找的具体步骤如下: 1、首先点击并进入手机浏览器; 2、然后点击页面上地址栏输入提示右边按钮,即可出现地址栏输入提示。 手机、全称为移动