ScrollView嵌套ListView引起的冲突

作者: 喂_balabala | 来源:发表于2017-12-29 11:24 被阅读5次

    ScrollView与ListView合用(正确计算Listview的高度)的问题解决
    首先,ListView不能直接用,要自定义一个,然后重写onMeasure()方法:

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

    第二步:写个计算listView每个Item的方法:

    public void setListViewHeightBasedOnChildren(ListView listView) {
      // 获取ListView对应的Adapter
      ListAdapter listAdapter = listView.getAdapter();
      if (listAdapter == null) {
       return;
      }
      int totalHeight = 0;
      for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
       View listItem = listAdapter.getView(i, null, listView);
       listItem.measure(0, 0); // 计算子项View 的宽高
       totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
      }
      ViewGroup.LayoutParams params = listView.getLayoutParams();
      params.height = totalHeight
        + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
      // listView.getDividerHeight()获取子项间分隔符占用的高度
      // params.height最后得到整个ListView完整显示需要的高度
      listView.setLayoutParams(params);
     }
    

    第三步:listview添加适配器后设置高度即可:

    listView.setAdapter(adapter);  
    new ListViewUtil().setListViewHeightBasedOnChildren(listView);  
    

    要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。

    需要手动把ScrollView滚动至最顶端,因为使用这个方法的话,默认在ScrollView顶端的项是ListView,具体原因不了解,求大神解答…可以在Activity中设置:

    mSlTimeLine.smoothScrollTo(0,0);
    mScrollView.scrollTo(x, y)首次初始化时无效果
    

    listview点击事件无法响应时要自定义listview
    自定义的listview一定要构造两个参数的构造方法,一个参数的运行异常。并重写onInterceptTouchEvent方法

       public SpinListView(Context context, AttributeSet attrs) {  
           super(context, attrs);  
           // TODO Auto-generated constructor stub  
       }  
    
    int flag = 0;
        float StartX;
        float StartY;
        float ScollX;
        float ScollY;
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
    //总是调用listview的touch事件处理
            onTouchEvent(ev);
            if(ev.getAction()==MotionEvent.ACTION_DOWN){
                StartX = ev.getX();
                StartY = ev.getY();
                return false;
            }
            if(ev.getAction()==MotionEvent.ACTION_MOVE){
                ScollX = ev.getX()-StartX;
                ScollY = ev.getY()-StartY;
    //判断是横滑还是竖滑,竖滑的话拦截move事件和up事件(不拦截会由于listview和scrollview同时执行滑动卡顿)
                if(Math.abs(ScollX)<Math.abs(ScollY)){
                    flag = 1;
                    return true;
                }
                return false;
            }
            if(ev.getAction()==MotionEvent.ACTION_UP){
                if(flag==1){
                    return true;
                }
                return false;
            }
            return super.onInterceptTouchEvent(ev);
        }
    

    scrollview隐藏滚动条

    ScrollView sView = (ScrollView)findViewById(R.id.sView);
    
    sView.setVerticalScrollBarEnabled(false); //禁用垂直滚动 sView.setHorizontalScrollBarEnabled(false); //禁用水平滚动
    
    一种是在XML的ScrollView布局中加入属性Android:scrollbars="none"
    

    解决ScrollView中有listView,造成不屏幕不停留在顶端的问题

    这个问题造成的原因 其实是加载ListView之后  ScrollView 的焦点异常   然后看上去被顶上去的
    
    其实有两个方法可以解决 这个问题 
    一个是
    scrollView.smoothScrollTo(0,20);
    顾名思义  这个是让屏幕滚动到顶端的意思。  可有的时候 必须用TabHost  等页面切换Fragment的时候 
    这个方法找不到合适的地方加    写在生命周期里  也可能不调用  所有就有了第二种方法
    在XML文件里   让listView失去焦点   
    只需要在父容器中加入这两条属性
    android:focusable="true"
    android:focusableInTouchMode="true"
    

    相关文章

      网友评论

        本文标题:ScrollView嵌套ListView引起的冲突

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