開發中遇到了這樣的一個問題,界面最外層是ScrollView,然后里面有嵌套了一個ListView還有其他可以獲取焦點的View,然后每次打開界面都會自動滾動到最底部,經過一番折騰,發現了一個簡單的方法,
獲取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
