
AndroID ListVIEw与ScrollVIEw冲突的解决方法总结
众所周知ListVIEw与ScrollVIEw都具有滚动能力,对于这样的VIEw控件,当ScrollVIEw与ListVIEw相互嵌套会成为一种问题:
问题一:ScrollVIEw与ListVIEw嵌套导致ListVIEw显示不全面
问题二:ScrollVIEw不能正常滑动
解决方式一:
ScrollVIEw+linearLayout+ListVIEw可以换成ScrollVIEw+linearLayout+linearLayout,对于开发中,ScrollVIEw所能滚动的样式形式各异,另外的话,ScrollVIEw所显示的内容肯定不会太多,因此这种方案是合理而且可选的
解决方式二:
同样是替换:ListVIEw具有headerVIEw与FooterVIEw2部分,因此,在非下拉刷新,上拉加载的需求中,完全可以使用ListVIEw来代替ScrollVIEw,因此是合理可选的方案
解决方式三:
主动计算和设置ListVIEw的高度,这样的结果最终得出类似解决方案一效果,具体来说缺点是大材小用,但也是合理的解决办法。
public class Utility { public static voID setListVIEwHeightBasedOnChildren(ListVIEw ListVIEw) { listadapter listadapter = ListVIEw.getAdapter(); if (listadapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listadapter.getCount(); i++) { VIEw ListItem = listadapter.getVIEw(i,null,ListVIEw); ListItem.measure(0,0); totalHeight += ListItem.getMeasuredHeight(); } VIEwGroup.LayoutParams params = ListVIEw.getLayoutParams(); params.height = totalHeight + (ListVIEw.getdivIDerHeight() * (listadapter.getCount() - 1)); ListVIEw.setLayoutParams(params); } } 解决方式四:
复写ScrollVIEw,从事件方向进行处理,缺点是灵活性不够好、
public class ListScrollVIEw extends ScrollVIEw { private List List = new ArrayList(); private int scrollpaddingtop; // scrollvIEw的顶部内边距 private int scrollpaddingleft;// scrollvIEw的左侧内边距 private int[] scrollLoaction = new int[2]; // scrollvIEw在窗口中的位置 private final static int UPGLIDE = 0; private final static int DOWNGLIDE = 1; private int glIDeState; public ListScrollVIEw(Context context,AttributeSet attrs) { super(context,attrs); } private int downY = 0; private int moveY = 0; @OverrIDe public boolean dispatchtouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downY = (int) ev.getY(); //System.out.println("actiondown" + ev.getY()); break; case MotionEvent.ACTION_MOVE: moveY= (int) ev.getY(); //System.out.println("move" + moveY + "down" + downY); if((moveY - downY) >= 0) { //System.out.println("'''''''''DOWNGLIDE'''''''''''"); glIDeState = DOWNGLIDE; } else { //System.out.println("'''''''''UPGLIDE'''''''''''"); glIDeState = UPGLIDE; } break; case MotionEvent.ACTION_UP: default: break; } return super.dispatchtouchEvent(ev); } @OverrIDe public boolean onIntercepttouchEvent(MotionEvent ev) { // 该事件的xy是以scrollvIEw的左上角为00点而不是以窗口为00点 int x = (int) ev.getX() + scrollLoaction[0]; int y = (int) ev.getY() + scrollLoaction[1]; for (int i = 0; i < List.size(); i++) { ListVIEw ListVIEw = List.get(i); int[] location = new int[2]; ListVIEw.getLocationInWindow(location); int wIDth = ListVIEw.getWIDth(); int height = ListVIEw.getHeight(); // 在ListvIEw的位置之内则可以滑动 if (x >= location[0] + scrollpaddingleft && x <= location[0] + scrollpaddingleft + wIDth && y >= location[1] + scrollpaddingtop && y <= location[1] + scrollpaddingtop + height) { //System.out.println(glIDeState); if(( (ListVIEw.getLastVisibleposition() == (ListVIEw.getCount()-1)) && (glIDeState == UPGLIDE) ) ) { //System.out.println("up"); break; } if(( (ListVIEw.getFirstVisibleposition() == 0) && (glIDeState == DOWNGLIDE))) { //System.out.println("down"); break; } return false; //让子控件直接处理 } } return super.onIntercepttouchEvent(ev); } @OverrIDe public boolean ontouchEvent(MotionEvent ev) { return super.ontouchEvent(ev); } private voID findAllListVIEw(VIEw vIEw) { if (vIEw instanceof VIEwGroup) { int count = ((VIEwGroup) vIEw).getChildCount(); for (int i = 0; i < count; i++) { if (!(vIEw instanceof ListVIEw)) { findAllListVIEw(((VIEwGroup) vIEw).getChildAt(i)); } } if (vIEw instanceof ListVIEw) { List.add((ListVIEw) vIEw); } } } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); scrollpaddingtop = gettop(); scrollpaddingleft = getleft(); getLocationInWindow(scrollLoaction); } @OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { super.onLayout(changed,l,t,r,b); if (this.getChildCount() != 1) { try { throw new ScrollException(); } catch (ScrollException e) { e.printstacktrace(); } } List.clear(); findAllListVIEw(this.getChildAt(0)); } } 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android ListView与ScrollView冲突的解决方法总结全部内容,希望文章能够帮你解决Android ListView与ScrollView冲突的解决方法总结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)