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);
}
私はWebViewのは、最後のスクロール位置に自動的にスクロールさせたいです?それは動作しません??
EDIT これが今の私の完全な活動である::::::::::::::::::::::::::::::::::: ::::::
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を返しますか? – Inducesmile
いいえ、浮動小数点値を返しますが、スクロールしません。 –