欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android ListView与ScrollView冲突的解决方法总结

程序员文章站 2023-11-24 18:31:28
android listview与scrollview冲突的解决方法总结 众所周知listview与scrollview都具有滚动能力,对于这样的view控件,当scro...

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)); 
 } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!