
正文
ScrollView嵌套RecyclerView、ScrollView嵌套Listview、ScrollView嵌套各种布局,默认不在顶部和回到顶部的解决方法;
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如果:
ScrollView.scrollTo(0,0);
ScrollView.fullScroll(View.FOCUS_UP) ;
ScrollView.smoothScrollTo(0, 0);
这三种方法都解决不了你的问题,那么请往下看;
布局有点复杂:
最外层是SwipeRefreshLayout嵌套ScrollView再嵌套LinearLayout再嵌套RecyclerView的;因为布局比较复杂,布局的位置还要求用户自己可以调整,使用的是LinearLayout动态添加View的方式,ScrollView让其可以滚动,SwioeRefreshLayout可以让界面下拉刷新;
但是功能都完成时发现ScrollView默认不在顶部;
1、设置了ScrollView.smoothScrollTo(0, 0);后默认在顶部了,但是用户调整布局后,我再动态添加后刷新一下布局ScrollView就又不在顶部了;我又在用户调整布局后,重新调用了一遍ScrollView.smoothScrollTo(0, 0);偶尔还是不在顶部;
2、我使用下面这种方法设置后,还是偶尔不能回到顶部;Handler的方法也是用过了,都不好用;
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView_Owner.smoothScrollTo(0, 0);
}
});
3、我在ScrollView的外层布局(SwipeRefreshLayout)设置:
android:focusable="true"
android:focusableInTouchMode="true"
然后再RecyclerView的外层布局(LinearLayout)设置:
android:descendantFocusability="blocksDescendants"
完美解决!!!
有空来写一下LinearLayout动态添加布局;
附上代码:
<com.saicmaxus.common.widget.VpSwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mSwipeRefresh_Owner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.saicmaxus.maxuslife.fragment.OwnerFragment2">
<!--
解决srcollView不在顶部的问题
android:focusable="true"
android:focusableInTouchMode="true"
-->
<ScrollView
android:id="@+id/scrollView_Owner"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/llOwner"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--
解决srcollView不在顶部的问题
android:descendantFocusability="blocksDescendants"
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <include
layout="@layout/view_is_not_owner"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</com.saicmaxus.common.widget.VpSwipeRefreshLayout>








