Android ScrollView自动滑动问题解决

项目中大量布局用到了ScrollView布局嵌套,而在切换数据是会发生内容自动滑动上移的情况,而此问题发生的原因是:ScrollView中子布局的的焦点的变化导致ScrollView自动的滑动 。这种情况多发生在子布局中嵌套ListView等的情况,原因是ListView抢占了ScrollView的焦点

解决方案:

第一种:

布局中添加属性,夺回焦点:(此方案最为简单有效,同样适用于下拉刷新组件com.handmark.pulltorefresh.library.PullToRefreshScrollView

 <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">
        </LinearLayout>
</ScrollView>


在LinearLayout中加入 
android:focusable=”true” 
android:focusableInTouchMode=”true” 
便能解决问题。

第二种:

调用ScrollView中的方法:void smoothScrollTo(intx,inty)

此种方法不推荐,有一定局限性,也较为繁琐,并且部分下拉刷新控件没有这个方法,无法调用

第三种:

可以使用代码让ListView失去焦点(更为繁琐,时间所限,本人并没有亲身使用,故此不做深究)

猜你喜欢

转载自blog.csdn.net/t_an123/article/details/73302570