Maintain WebView content scroll position on orientation change

前端 未结 8 671
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 02:04

The browsers in Android 2.3+ do a good job at maintaining the scrolled position of content on an orientation changed.

I\'m trying to achieve the same thing for a We

相关标签:
8条回答
  • 2020-12-23 02:13

    onPageFinished may not be called because you are not reloading the page, you are just changing the orientation, not sure if this causes a reload or not.

    Try using scrollTo in the onConfigurationChanged method of your activity.

    0 讨论(0)
  • 2020-12-23 02:14

    The aspect change will most likely always cause the current location in your WebView to not be the right location to scroll to afterwards. You could be sneaky and determine the top most visible element in the WebView and after an orientation change implant an anchor at that point in the source and redirect the user to it...

    0 讨论(0)
  • 2020-12-23 02:14

    I used the partial solution described in the original post, but instead put the javascript snippet inside onPageStarted(), instead of onPageFinished(). Seems to work for me with no jumping.

    My use case is slightly different: I'm trying to keep the horizontal position after the user refreshes the page.

    But I've been able to use your solution and it works perfectly for me :)

    0 讨论(0)
  • 2020-12-23 02:17

    In the partial solution described in the original post, you can improve the solution if change the following code

    private float calculateProgression(WebView content) {
        float contentHeight = content.getContentHeight();
        float currentScrollPosition = content.getScrollY();
        float percentWebview = currentScrollPosition/ contentHeight;
        return percentWebview;
    }
    

    due to the top position of the component, when you are using content.getTop () is not necessary; So what it does is add to the actual position of the scroll Y position where the component is located with respect to it parent and runs down the content. I hope my explanation is clear.

    0 讨论(0)
  • 2020-12-23 02:20

    The way that we have managed to achieve this to have a local reference to the WebView within our Fragments.

    /**
    * WebView reference
    */
    private WebView webView;
    

    Then setRetainInstance to true in onCreate

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    
        // Argument loading settings
    }
    

    Then when onCreateView is called, return the existing local instance of the WebView, otherwise instantiate a new copy setting up the content and other settings. The key step he is when reattaching the WebView to remove the parent which you can see in the else clause below.

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
    
        final View view;
        if (webView == null) {
    
            view = inflater.inflate(R.layout.webview_dialog, container);
    
            webView = (WebView) view.findViewById(R.id.dialog_web_view);
    
            // Setup webview and content etc... 
        } else {
            ((ViewGroup) webView.getParent()).removeView(webView);
            view = webView;
        }
    
        return view;
    }
    
    0 讨论(0)
  • 2020-12-23 02:21

    to calculate the right percentage of WebView, it is important to count mWebView.getScale() also. Actually, return value of getScrollY() is respectively with mWebView.getContentHeight()*mWebView.getScale().

    0 讨论(0)
提交回复
热议问题