【问题标题】:WebView scroll position?WebView 滚动位置?
【发布时间】:2016-10-14 21:25:36
【问题描述】:

我有这个代码::

    //Scroll position from last time
    String scrollPos = intent.getExtras().getString("SCROLL");
    //chapter position from last time (this is a book app)
    String chapterPos = intent.getExtras().getString("EXTRA");
    //this methode changes the webView content according to last chapter visited (it works fine)
    doItNow(chapterPos);

    // PROBLEM IS HERE !!!!
    if(scrollPos!=null) {

        float webviewsize = webView.getContentHeight() - webView.getTop();
        float positionInWV = webviewsize * (Float.parseFloat(scrollPos));
        int positionY = Math.round(webView.getTop() + positionInWV);
        webView.scrollTo(0, positionY);
    }

我希望我的 webView 自动滚动到最后一个滚动位置??但它不起作用??

编辑 这是我现在的全部活动 ::::::::::::::::::::::::::::::::::::::::

public class book extends AppCompatActivity {

private String extra;
private WebView webView;
private TextView textView;
private Intent intent;
private LinearLayout Linear1;
private RelativeLayout Relative1;
private int colorId;
private LayoutInflater inflater;
private View layout;
private TextView text;
private Toast toast;
public int flag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book);

    textView = (TextView)findViewById(R.id.textView3);
    webView = (WebView)this.findViewById(R.id.webView);
    Linear1 = (LinearLayout)findViewById(R.id.Linear1);
    Relative1 = (RelativeLayout)findViewById(R.id.Relative1);

    Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/arabicfont.otf");
    textView.setTypeface(face);

    intent = getIntent();
    colorId = intent.getIntExtra("COLOR_EXTRA", colorId);

    Linear1.setBackgroundResource(colorId);
    Relative1.setBackgroundResource(colorId);

    String chapterPos = intent.getExtras().getString("EXTRA");

    doItNow(chapterPos);

    webView.setWebViewClient(new WebViewClient(){

                                     @Override
                                     public void onPageFinished(WebView view, String url) {
                                         super.onPageFinished(view, url);
                                         String scrollPos = intent.getExtras().getString("SCROLL");
                                         Toast.makeText(book.this, scrollPos, Toast.LENGTH_SHORT).show();
                                         float webviewsize = webView.getContentHeight() - webView.getTop();
                                         float positionInWV = webviewsize * (Float.parseFloat(scrollPos));
                                         int positionY = Math.round(webView.getTop() + positionInWV);
                                         webView.scrollTo(0, positionY);
                                     }

                                 }
    );

}

public void sectionsFnc(View view) {
    Intent intent = new Intent(book.this, storyZero.class);
    intent.putExtra("COLOR_EXTRA", colorId);
    startActivity(intent);
}

public void backFunc(View view) {
    Intent intent = new Intent(book.this, FirstScreen.class);
    startActivity(intent);
}

public void nextStory(View view) {

    int iExtra = Integer.parseInt(extra) + 1;

    if(iExtra<=10) {
        extra = String.valueOf(iExtra);
        doItNow(extra);
    } else {
        inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toast_root));

        text = (TextView) layout.findViewById(R.id.text);
        text.setText("لقد وصلت لنهاية الكتاب!");

        toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

public void preStory(View view) {
    int iExtra = Integer.parseInt(extra) - 1;
    if(iExtra>=0) {
        extra = String.valueOf(iExtra);
        doItNow(extra);
    } else {
        inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toast_root));

        text = (TextView) layout.findViewById(R.id.text);
        text.setText("أنت في بداية الكتاب!");

        toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

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

@Override
protected void onStop() {
    float position = calculateProgression(webView);
    SharedPreferences sharePref = getSharedPreferences("webviewScroll", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharePref.edit();
    editor.putFloat("SCROLLPOS", position);
    editor.putInt("CHAPTER", flag);
    editor.commit();
    super.onStop();
}

public void doItNow(String mExtra) {

    extra = mExtra;

    if(extra.equals("0")) {
        webView.loadUrl("file:///android_asset/1.htm");
        flag = 0;

    } else if(extra.equals("1")) {
        webView.loadUrl("file:///android_asset/2.htm");
        flag = 1;

    } else if(extra.equals("2")) {
        webView.loadUrl("file:///android_asset/3.htm");
        flag = 2;

    } else if(extra.equals("3")) {
        webView.loadUrl("file:///android_asset/4.htm");
        flag = 3;
    } else if(extra.equals("4")) {
        webView.loadUrl("file:///android_asset/5.htm");
        flag = 4;

    } else if(extra.equals("5")) {
        webView.loadUrl("file:///android_asset/6.htm");
        flag = 5;

    } else if(extra.equals("6")) {
        webView.loadUrl("file:///android_asset/7.htm");
        flag = 6;

    } else if(extra.equals("7")) {
        webView.loadUrl("file:///android_asset/8.htm");
        flag = 7;

    } else if(extra.equals("8")) {
        webView.loadUrl("file:///android_asset/9.htm");
        flag = 8;

    } else if(extra.equals("9")) {
        webView.loadUrl("file:///android_asset/10.htm");
        flag = 9;

    } else if(extra.equals("10")) {
        webView.loadUrl("file:///android_asset/11.htm");
        flag = 10;
    }
}

}

【问题讨论】:

  • scrollPos 是否返回 null?
  • 不,它返回一个浮点值但它不滚动

标签: android android-intent scroll webview position


【解决方案1】:

请粘贴您的完整代码以识别问题,但我猜您在 webview 完成加载页面之前调用了 scrollTo,因此在这种情况下您必须使用

yourWebView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            yourWebView.scrollTo(xVal, yVal);
        }

    }
    );

如果您的页面包含图像,您可以使用它

yourWebView.setPictureListener(new PictureListener() {

        @Override
        public void onNewPicture(WebView view, Picture picture) {
             yourWebView.scrollTo(xVal, yVal);

        }
    });

【讨论】:

  • 怎么做 >> 我用完整的活动编辑了我的帖子
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
相关资源
最近更新 更多