2016-10-14 2 views
0

이 코드 ::WebView 스크롤 위치?

//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); 
    } 

나는 내 웹보기 마지막 스크롤 위치로 자동으로 스크롤 할가? 하지만 작동하지 않습니다 ??

편집 이 지금은 내 전체 활동이다 ::::::::::::::::::::::::::::::::::: ::::::

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; 
    } 
} 

} 
+0

scrollPos는 null을 반환합니까? – Inducesmile

+0

아니요, float 값을 반환하지만 스크롤하지 않습니다 –

답변

0

문제를 식별하는 전체 코드를 붙여 넣하지만

를 사용할 필요가이 경우에 그래서 웹보기가 페이지를로드 완료하기 전에 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); 

     } 
    }); 
+0

어떻게 수행합니까 >> 전체 활동으로 내 게시물을 편집했습니다. –