scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, 0);
}
});
对于其他需要解决方案的人,这里是:
/**
* This function will scroll and bring a view to the top of scrollView
*
* @param view target view
*/
public static void scrollToView(View rootView, ScrollView scrollView, View view) {
rootView.clearFocus();
int[] locationOfView = new int[2];
view.getLocationOnScreen(locationOfView);
int viewY = locationOfView[1];
int[] locationOfScrollView = new int[2];
scrollView.getLocationOnScreen(locationOfScrollView);
int scrollY = locationOfScrollView[1];
scrollView.post(() -> scrollView.smoothScrollTo(0, viewY + scrollView.getScrollY() - scrollY));
}