美文网首页
ScrollView嵌套GridView踩坑

ScrollView嵌套GridView踩坑

作者: Qsy_Zer0 | 来源:发表于2016-09-30 17:07 被阅读398次

GridView显示不完全--->解决方法
1.用RecyclerView
2.自定义GridView,重写onMeasure()方法

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
super.onMeasure(widthMeasureSpec, expandSpec); 
}

选用解决方法2然后会发现GridView 在当前界面中(只要有一部分在屏幕中即会触发)导致 ScrollView根据GridView位置发生相对滚动--->解决方案
1.topView.requestFocus();//顶部view获取焦点
2.gridView.setFocusable(false); //让GridView不能获得焦点(xml中设置无效)
3.scrollView.scrollTo(0,0)//注意使用时机,感觉很傻直接没试
原因应该是
scrollToChild根据computeScrollDeltaToGetChildRectOnScreen的返回值来计算滚动的位置
重载computeScrollDeltaToGetChildRectOnScreen让其返回0 会导致ScrollView内布局产生变化时,不能正确滚动到focus child位置
当然你不需要这个功能的话 重载computeScrollDeltaToGetChildRectOnScreen也可以
computeScrollDeltaToGetChildRectOnScreen大致是 根据当前 scrollY和focus child 的 rect.bottom 去计算要滚到哪

相关文章

网友评论

      本文标题:ScrollView嵌套GridView踩坑

      本文链接:https://www.haomeiwen.com/subject/ewzlyttx.html