在android的實際開發中,會要這樣的情況。就是scrollView佈局中,嵌套了很多佈局,但是ScrollView的佈局裏面只能嵌套一個大的佈局,而大的佈局中在嵌套各種其他的佈局!
可是,實際中,如ScrollView中的大的佈局中嵌套了一個ListView佈局,當ListView中的數據進行更新後,ScrollView就會自動滾動了!非常影響實際使用。那麼如何解決呢?其實很簡單!就兩步!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/friend_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/basecolor"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/basecolor"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
......
</LinearLayout>
</ScrollView>
仔細看下,發現了什麼沒有?
android:focusable="true"
android:focusableInTouchMode="true"
就是因爲這兩句,在scrollView中的第一層中,設置這兩個屬性後,不管ScrollView中的內容與控件長度如何變化,scrollView始終保持在頂端!