Webview in Scrollview

后端 未结 6 887
南旧
南旧 2020-11-29 02:40

I have to place WebView into ScrollView. But I have to put some views into the same scrollview before webview. So it looks like this:



        
6条回答
  •  心在旅途
    2020-11-29 03:30

    None of this answers work for me, so here is my solution. First of all we need to override WebView to have ability to handle its scrolling. You can find how to do it here: https://stackoverflow.com/a/14753235/1285670

    Then create your xml with two view, where one is WebView and another is your title layout.

    This is my xml code:

    
    
      
    
      
    
          ////some stuff
    
        
    
    
    

    Next is handle WebView scroll and move title appropriate to scroll value. You must use NineOldAndroids here.

    @Override
    public void onScroll(int l, int t, int oldl, int oldt) {
        if (t < mTitleLayout.getHeight()) {
            ViewHelper.setTranslationY(mTitleLayout, -t);
        } else if (oldt < mTitleLayout.getHeight()) {
            ViewHelper.setTranslationY(mTitleLayout, -mTitleLayout.getHeight());
        }
    }
    

    And you must add padding to your WebView content, so it will not overlay title:

    v.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @SuppressLint("NewApi")
            @Override
            public void onGlobalLayout() {
                if (mTitleLayout.getHeight() != 0) {
                    if (Build.VERSION.SDK_INT >= 16)
                        v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
    
                    mWebView.loadDataWithBaseURL(null, "
    " + mPost.getContent() + "
    ", "text/html", "UTF8", null); } });

提交回复
热议问题