恢复活动时,我需要帮助保存我的scrollview的位置。 我有一个包含一些按钮的长滚动视图。当我点击这些按钮时,会打开新的活动。当我恢复scrollview的活动时,scrollview从顶部开始,用户必须再次滚动。这很烦人。
如何保存滚动视图位置?
我发现了这一点,但是行Util.Log("X:" + scrollX + " Y:" + scrollY);
不起作用:
// two static variable,
public static int scrollX = 0;
public static int scrollY = -1;
//update & save their value on onPause & onResume.
@Override
protected void onPause()
{
super.onPause();
scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();
}
@Override
protected void onResume()
{
Util.Log("X:" + scrollX + " Y:" + scrollY);
super.onResume();
//this is important. scrollTo doesn't work in main thread.
scrollView.post(new Runnable()
{
@Override
public void run()
{
scrollView.scrollTo(scrollX, scrollY);
}
});
}
感谢您的帮助! :d
编辑:
如果我删除该行,我会收到此错误:
01-01 17:29:11.023: E/AndroidRuntime(1967): FATAL EXCEPTION: main
01-01 17:29:11.023: E/AndroidRuntime(1967): Process: com.example.appquiz, PID: 1967
01-01 17:29:11.023: E/AndroidRuntime(1967): java.lang.RuntimeException: Unable to resume activity {com.example.appquiz/com.example.appquiz.Categories}: java.lang.NullPointerException
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2808)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2837)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.os.Looper.loop(Looper.java:136)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-01 17:29:11.023: E/AndroidRuntime(1967): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 17:29:11.023: E/AndroidRuntime(1967): at java.lang.reflect.Method.invoke(Method.java:515)
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 17:29:11.023: E/AndroidRuntime(1967): at dalvik.system.NativeStart.main(Native Method)
01-01 17:29:11.023: E/AndroidRuntime(1967): Caused by: java.lang.NullPointerException
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.example.appquiz.Categories.onResume(Categories.java:82)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.Activity.performResume(Activity.java:5319)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2798)
01-01 17:29:11.023: E/AndroidRuntime(1967): ... 12 more
我的整个代码:
public static int scrollX = 0;
public static int scrollY = -1;
ScrollView scrollView = (ScrollView) findViewById(R.id.Scrollview);
//blabla
onCreate //...
@Override
protected void onPause() {
// TODO Auto-generated method stub
scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
scrollView.post(new Runnable(){
@Override
public void run(){
scrollView.scrollTo(scrollX, scrollY);}});}
答案 0 :(得分:3)
将此代码添加到inflateView方法的末尾,请确保R.id.Scrollview是正确的ID
scrollView = (ScrollView) findViewById(R.id.Scrollview);
顺便说一下,你不应该使用post方法来做到这一点。滚动视图的最佳方法是执行以下操作:
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
scrollView.scrollTo(0, 0);
}
});
如果这没有用,请告诉我们这是什么行:(com.example.appquiz.Categories.onResume(Categories.java:82))
答案 1 :(得分:0)
Util.Log(“X:”+ scrollX +“Y:”+ scrollY);删除此代码dude这只是记录代码而与功能无关