ScrollView中嵌套 GridView 导致 ScrollView默认不停留在顶部的解决方案和分析

来源:互联网 发布:vscode php注释插件 编辑:程序博客网 时间:2024/04/30 15:26

原文地址:http://www.tuicool.com/articles/yYvIJn

ScrollView中嵌套 GridView 导致 ScrollView默认不停留在顶部的解决方案和分析

发生情况大概是我在ScrollView底部放了个GridView 来实现一个类似9宫格效果的展示.

Grid固定为2排,每排3个.固定为6个…所以没有性能问题,不需要重用,所以直接用GridView了..

只是为了方便和数据对应处理.

然后出现的状况是,当我获取完数据并调用notifyDataSetChanged();后 ScrollView自动滚到了最底部,也就是GridView所在的位置.

百度了一下,获取了一些解决方案

  1. view.requestFocus(); 让界面顶部的某一个View获取focus
  2. grid.setFocusable(false); 让Grid不能获得focus

  3. 手动scrollto(0,0)

  4. 重写ScrollView中的computeScrollDeltaToGetChildRectOnScreen,让该方法返回0目前简单的用setFocusable(false)解决了该问题

分析一下这个问题产生的原因. 从解决方案反推,这个问题产生和 focus有关系

一个猜测是 notifyDataSetChanged()之后,grid由于加载了数据的关系高度产生了变化

这导致了ScrollView内部重新走了 onLayout / onMeaure 流程 在这个流程中 ScrollView会将自身滚动到 获得 focus 的 child 位置上面关于focus的解决方案即是从这个角度去解决问题

手动scrollto(0,0)是个比较烂的办法

而重写ScrollView中的computeScrollDeltaToGetChildRectOnScreen跟踪一下调用链

Java

protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); mIsLayoutDirty = false; // Give a child focus if it needs it if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) { scrollToChild(mChildToScrollTo); } ...}

可以看到 onLayout 的时候确实会将ScrollView滚动到focus child位置

private void scrollToChild(View child) { child.getDrawingRect(mTempRect); /* Offset from child's local coordinates to ScrollView coordinates */ offsetDescendantRectToMyCoords(child, mTempRect); int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect); if (scrollDelta != 0) { scrollBy(0, scrollDelta); }}

而scrollToChild会根据computeScrollDeltaToGetChildRectOnScreen的返回值来计算滚动的位置

重载computeScrollDeltaToGetChildRectOnScreen让其返回0 会导致ScrollView内布局产生变化时,不能正确滚动到focus child位置

当然你不需要这个功能的话 重载computeScrollDeltaToGetChildRectOnScreen也可以

至于computeScrollDeltaToGetChildRectOnScreen代码太长就不贴了

大致是 根据当前 scrollY和focus child 的 rect.bottom 去计算要滚到哪

逻辑理顺以后觉得这个问题也没什么奇怪的.

现在还剩个问题 不是很明白GridView为何会默认获得focus

本来想写几个demo测试下,结果公司电脑太TM卡了..算了…记录下…有空再看..
0 0
原创粉丝点击