界面為ScrollView時打開界面會自動滾動到底部之解決方法


開發中遇到了這樣的一個問題,界面最外層是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






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM