我想知道是否可以在活动中创建按钮以将滚动视图跳转到相应的信息。有点像:
Button 1
Button 2
Button 3
paragraph 1
paragraph 2
paragraph 3
因此,当选择按钮1时,会将您带到第1段的开头,依此类推。通过这种方式,用户不必向下滚动以查找相关段落/信息或开始新活动。 (类似于一些网页上有很多关于页面顶部标题的信息,可以将您带到同一页面上的相关信息。)
好的,这就是我尝试过的方法:
final View rootView = inflater.inflate(R.layout.fragment_snakes, container, false);
Button button = (Button) rootView.findViewById(R.id.Button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
mScrollView = (ScrollView) rootView.findViewById(R.string.TextView1);
mScrollView.smoothScrollTo(R.id.TextView1, 0);
}
});
然后我收到这个错误:
11-05 09:28:43.722: E/AndroidRuntime(30741): java.lang.NullPointerException
11-05 09:28:43.722: E/AndroidRuntime(30741): at com.test.testpark.StestFragment$1.onClick(StestFragment.java:34)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.view.View.performClick(View.java:4637)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.view.View$PerformClick.run(View.java:19422)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.os.Handler.handleCallback(Handler.java:733)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.os.Handler.dispatchMessage(Handler.java:95)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.os.Looper.loop(Looper.java:136)
11-05 09:28:43.722: E/AndroidRuntime(30741): at android.app.ActivityThread.main(ActivityThread.java:5586)
11-05 09:28:43.722: E/AndroidRuntime(30741): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 09:28:43.722: E/AndroidRuntime(30741): at java.lang.reflect.Method.invoke(Method.java:515)
11-05 09:28:43.722: E/AndroidRuntime(30741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
11-05 09:28:43.722: E/AndroidRuntime(30741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
11-05 09:28:43.722: E/AndroidRuntime(30741): at dalvik.system.NativeStart.main(Native Method)
我可以遵循的教程吗? 有什么建议吗?
修改
11-05 10:41:36.770: E/AndroidRuntime(3630): java.lang.NullPointerException
11-05 10:41:36.770: E/AndroidRuntime(3630): at com.test.testpark.StestFragment$1.onClick(StestFragment.java:34)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.view.View.performClick(View.java:4637)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.view.View$PerformClick.run(View.java:19422)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.os.Handler.handleCallback(Handler.java:733)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.os.Handler.dispatchMessage(Handler.java:95)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.os.Looper.loop(Looper.java:136)
11-05 10:41:36.770: E/AndroidRuntime(3630): at android.app.ActivityThread.main(ActivityThread.java:5586)
答案 0 :(得分:2)
与scrollTo(int,int)类似,但是可以平滑滚动而不是立即滚动。
参数:x在X轴上滚动的位置 y在Y轴上滚动的位置
所以你不应该使用Textview id(R.id.TextView1)而是使用textview位置,如下所示:
final View rootView = inflater.inflate(R.layout.fragment_snakes, container, false);
Button button = (Button) rootView.findViewById(R.id.Button1);
TextView paragraph1 = (TextView) rootView.findViewById(R.id.TextView1);
ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.ScrollView);
button.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
scrollView.smoothScrollTo(0,paragraph1.getTop());
}
});
答案 1 :(得分:0)
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
mScrollView.smoothScrollTo(0, mParagraph1.getTop());
}
}