用户登录
用户注册

分享至

Android之ScrollView嵌套ListView和GridView冲突的解决方法

  • 作者: ----27414689
  • 来源: 51数据库
  • 2021-10-19
那么里面的scrollview高度计算就会出现问题。我们也就无法得到想要的效果。
核心解决方案: 重写listview或者gridview的onmesure 方法。
复制代码 代码如下:

public class mylistview extends listview {
        public mylistview(context context) {
                super(context);
        }
        public mylistview(context context, attributeset attrs) {
                super(context, attrs);
        }
        public mylistview(context context, attributeset attrs, int defstyle) {
                super(context, attrs, defstyle);
        }
        @override
        protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
                int expandspec = measurespec.makemeasurespec(integer.max_value >> 2,
                                measurespec.at_most);
                super.onmeasure(widthmeasurespec, expandspec);
        }
}

gridview
复制代码 代码如下:

public class mygridview extends gridview {  
    private boolean havescrollbar = true;  
    public mygridview(context context) {  
        super(context);  
    }  
    public mygridview(context context, attributeset attrs) {  
        super(context, attrs);  
    }  
    public mygridview(context context, attributeset attrs, int defstyle) {  
        super(context, attrs, defstyle);  
    }  
    /** 
     * 设置是否有scrollbar,当要在scollview中显示时,应当设置为false。 默认为 true 
     *  
     * @param havescrollbars 
     */  
    public void sethavescrollbar(boolean havescrollbar) {  
        this.havescrollbar = havescrollbar;  
    }  
    @override  
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {  
        if (havescrollbars == false) {  
            int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most);  
            super.onmeasure(widthmeasurespec, expandspec);  
        } else {  
            super.onmeasure(widthmeasurespec, heightmeasurespec);  
        }  
    }  
}
软件
前端设计
程序设计
Java相关