开发过程中遇到了一个问题,布局中含有ScrollView,并且里面又嵌套了一个ListView时,每次打开界面都会自动滚动到最底部
解决方法如下:
获取ScrollView的任意一个上层view,然后调用如下方法:
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
或者将scrollview包裹的内容设置上以下两个属性
android:focusable=”true”
android:focusableInTouchMode=”true”
代码如下:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true" />
</ScrollView