1. 程式人生 > >ScrollView自動滾動(內部控制元件內容發生變化時)的問題

ScrollView自動滾動(內部控制元件內容發生變化時)的問題

看到很多部落格說在ScrollView的子佈局里加入

android:focusable="true"
android:focusableInTouchMode="true" 

本人是一個Activity四個fragment,作為頁卡切換。
在每個fragment的ScrollView下同上述程式碼寫入,發現只有第一個fragment的ScrollView沒有自動滾動,
第二個依然滾動,再次百度了下,找到了解決方法。PS:部落格地址我忘了。。。後來嘗試再找,然而沒找到。
解決方法:重寫ScrollView,在其內重寫如下方法

import android.content.Context;
import
android.graphics.Rect; import android.util.AttributeSet; import android.widget.ScrollView; /** * Created by lee on 16-8-5. */ public class NoAutoSlideScrollView extends ScrollView { public NoAutoSlideScrollView(Context context) { super(context); } public NoAutoSlideScrollView(Context context, AttributeSet attrs) { super
(context, attrs); } public NoAutoSlideScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) { return 0; } }

複寫computeScrollDeltaToGetChildRectOnScreen,匯入Rect類即可,之後xml裡引用下