How to Hide ActionBar/Toolbar While Scrolling Down in Webview

前端 未结 3 1940
心在旅途
心在旅途 2020-12-02 12:58

In Google chrome and play store. the app can hide the actionbar while scrolling and allows the user to Browse conveniently. Please Help me to do like this.

I\'ve use

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 13:23

    Well I have implemented by CustomWebView and GestureDetector:

    CustomWebView.java:

    public class CustomWebView extends WebView {
        private GestureDetector gestureDetector;
        public CustomWebView(Context context) {
            super(context);
        }
        public CustomWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
        }
    
        public void setGestureDetector(GestureDetector gestureDetector) {
            this.gestureDetector = gestureDetector;
        }
    }
    

    web_fragment.xml:

    
    
    
       
    
    
    

    CustomeGestureDetector clss for Gesture Detection (I have added in Fragment):

    private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if(e1 == null || e2 == null) return false;
                if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
                else {
                    try {
                        if(e1.getY() - e2.getY() > 20 ) {
                                // Hide Actionbar
                            getSupportActionBar().hide();
                            customWebView.invalidate();
                           return false;
                        }
                        else if (e2.getY() - e1.getY() > 20 ) {
                                // Show Actionbar
                            getSupportActionBar().show();
                            customWebView.invalidate();
                           return false;
                        }
    
                    } catch (Exception e) {
                        customWebView.invalidate();
                    }
                    return false;
                }
    
    
            }
        }
    

    WebFragment.java:

    private CustomWebView customWebView;
    
    customWebView= (CustomWebView) view.findViewById(R.id.customWebView);
    
    customWebView.setGestureDetector(new GestureDetector(new CustomeGestureDetector()));
    

    It works fine for me, hope it would help you.

提交回复
热议问题